Spaces:
Runtime error
Runtime error
acecalisto3
commited on
Commit
•
c1a05c3
1
Parent(s):
d7dff8e
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from typing import List, Dict, Optional
|
2 |
-
from
|
3 |
Code,
|
4 |
Prompt,
|
5 |
AppType,
|
@@ -31,15 +31,10 @@ from huggingface_hub import InferenceClient
|
|
31 |
class Agent:
|
32 |
def __init__(self, prompts: Dict[str, any]):
|
33 |
self.prompts = prompts
|
34 |
-
self.client = InferenceClient(
|
35 |
-
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
36 |
-
)
|
37 |
|
38 |
def process(self, user_input: str) -> str:
|
39 |
-
"""
|
40 |
-
Processes the user's input and generates code.
|
41 |
-
"""
|
42 |
-
|
43 |
# Parse the user's input
|
44 |
app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial = self.parse_input(user_input)
|
45 |
|
@@ -60,38 +55,26 @@ class Agent:
|
|
60 |
return f"Code: {code}\nFiles: {files}"
|
61 |
|
62 |
def parse_input(self, user_input: str) -> tuple:
|
63 |
-
"""
|
64 |
-
Parses the user's input and extracts the relevant information.
|
65 |
-
"""
|
66 |
-
|
67 |
# Extract the app type
|
68 |
app_type = self.extract_app_type(user_input)
|
69 |
-
|
70 |
# Extract the app name
|
71 |
app_name = self.extract_app_name(user_input)
|
72 |
-
|
73 |
# Extract the app description
|
74 |
app_description = self.extract_app_description(user_input)
|
75 |
-
|
76 |
# Extract the app features
|
77 |
app_features = self.extract_app_features(user_input)
|
78 |
-
|
79 |
# Extract the app dependencies
|
80 |
app_dependencies = self.extract_app_dependencies(user_input)
|
81 |
-
|
82 |
# Extract the app space
|
83 |
app_space = self.extract_app_space(user_input)
|
84 |
-
|
85 |
# Extract the app tutorial
|
86 |
app_tutorial = self.extract_app_tutorial(user_input)
|
87 |
|
88 |
return app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial
|
89 |
|
90 |
def extract_app_type(self, user_input: str) -> AppType:
|
91 |
-
"""
|
92 |
-
Extracts the app type from the user's input.
|
93 |
-
"""
|
94 |
-
|
95 |
# Check if the user specified a specific app type
|
96 |
if "web app" in user_input:
|
97 |
return AppType.WEB_APP
|
@@ -101,144 +84,124 @@ class Agent:
|
|
101 |
return AppType.STREAMLIT_APP
|
102 |
elif "react app" in user_input:
|
103 |
return AppType.REACT_APP
|
104 |
-
|
105 |
# Otherwise, assume the user wants a web app
|
106 |
return AppType.WEB_APP
|
107 |
|
108 |
def extract_app_name(self, user_input: str) -> str:
|
109 |
-
"""
|
110 |
-
Extracts the app name from the user's input.
|
111 |
-
"""
|
112 |
-
|
113 |
# Find the substring "app name is:"
|
114 |
start_index = user_input.find("app name is:") + len("app name is:")
|
115 |
-
|
116 |
# Find the end of the app name
|
117 |
end_index = user_input.find(".", start_index)
|
118 |
-
|
119 |
# Extract the app name
|
120 |
app_name = user_input[start_index:end_index].strip()
|
121 |
-
|
122 |
return app_name
|
123 |
|
124 |
def extract_app_description(self, user_input: str) -> str:
|
125 |
-
"""
|
126 |
-
Extracts the app description from the user's input.
|
127 |
-
"""
|
128 |
-
|
129 |
# Find the substring "app description is:"
|
130 |
start_index = user_input.find("app description is:") + len("app description is:")
|
131 |
-
|
132 |
# Find the end of the app description
|
133 |
end_index = user_input.find(".", start_index)
|
134 |
-
|
135 |
# Extract the app description
|
136 |
app_description = user_input[start_index:end_index].strip()
|
137 |
-
|
138 |
return app_description
|
139 |
|
140 |
def extract_app_features(self, user_input: str) -> List[str]:
|
141 |
-
"""
|
142 |
-
Extracts the app features from the user's input.
|
143 |
-
"""
|
144 |
-
|
145 |
# Find the substring "app features are:"
|
146 |
start_index = user_input.find("app features are:") + len("app features are:")
|
147 |
-
|
148 |
# Find the end of the app features
|
149 |
end_index = user_input.find(".", start_index)
|
150 |
-
|
151 |
# Extract the app features
|
152 |
app_features_str = user_input[start_index:end_index].strip()
|
153 |
-
|
154 |
# Split the app features string into a list
|
155 |
app_features = app_features_str.split(", ")
|
156 |
-
|
157 |
return app_features
|
158 |
|
159 |
def extract_app_dependencies(self, user_input: str) -> List[str]:
|
160 |
-
"""
|
161 |
-
Extracts the app dependencies from the user's input.
|
162 |
-
"""
|
163 |
-
|
164 |
# Find the substring "app dependencies are:"
|
165 |
start_index = user_input.find("app dependencies are:") + len("app dependencies are:")
|
166 |
-
|
167 |
# Find the end of the app dependencies
|
168 |
end_index = user_input.find(".", start_index)
|
169 |
-
|
170 |
# Extract the app dependencies
|
171 |
app_dependencies_str = user_input[start_index:end_index].strip()
|
172 |
-
|
173 |
# Split the app dependencies string into a list
|
174 |
app_dependencies = app_dependencies_str.split(", ")
|
175 |
-
|
176 |
return app_dependencies
|
177 |
|
178 |
def extract_app_space(self, user_input: str) -> Optional[Space]:
|
179 |
-
"""
|
180 |
-
Extracts the app space from the user's input.
|
181 |
-
"""
|
182 |
-
|
183 |
# Find the substring "app space is:"
|
184 |
start_index = user_input.find("app space is:") + len("app space is:")
|
185 |
-
|
186 |
# Find the end of the app space
|
187 |
end_index = user_input.find(".", start_index)
|
188 |
-
|
189 |
# Extract the app space
|
190 |
app_space_str = user_input[start_index:end_index].strip()
|
191 |
-
|
192 |
# Create a Space object
|
193 |
app_space = Space(space=app_space_str)
|
194 |
-
|
195 |
return app_space
|
196 |
|
197 |
def extract_app_tutorial(self, user_input: str) -> Optional[Tutorial]:
|
198 |
-
"""
|
199 |
-
Extracts the app tutorial from the user's input.
|
200 |
-
"""
|
201 |
-
|
202 |
# Find the substring "app tutorial is:"
|
203 |
start_index = user_input.find("app tutorial is:") + len("app tutorial is:")
|
204 |
-
|
205 |
# Find the end of the app tutorial
|
206 |
end_index = user_input.find(".", start_index)
|
207 |
-
|
208 |
# Extract the app tutorial
|
209 |
app_tutorial_str = user_input[start_index:end_index].strip()
|
210 |
-
|
211 |
# Create a Tutorial object
|
212 |
app_tutorial = Tutorial(tutorial=app_tutorial_str)
|
213 |
-
|
214 |
return app_tutorial
|
215 |
|
216 |
def generate_code(self, prompt: Prompt) -> Code:
|
217 |
-
"""
|
218 |
-
Generates code using the Llama model.
|
219 |
-
"""
|
220 |
-
|
221 |
# Send the prompt to the Llama model
|
222 |
response = self.client(prompt.prompt)
|
223 |
-
|
224 |
# Extract the generated code
|
225 |
code = response["generated_text"]
|
226 |
code = code.replace("```", "")
|
227 |
code = code.replace("```", "")
|
228 |
-
|
229 |
# Create a Code object
|
230 |
code = Code(code=code, language="python")
|
231 |
-
|
232 |
return code
|
233 |
|
234 |
def generate_files(self, app_type: AppType, app_name: str, app_description: str, app_features: List[str], app_dependencies: List[str], app_space: Optional[Space] = None, app_tutorial: Optional[Tutorial] = None) -> List[File]:
|
235 |
-
"""
|
236 |
-
Generates files for the application.
|
237 |
-
"""
|
238 |
-
|
239 |
# Generate files based on the app type
|
240 |
files = self.prompts["generateFiles"](
|
241 |
app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial
|
242 |
)
|
243 |
-
|
244 |
return files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from typing import List, Dict, Optional
|
2 |
+
from custom_types import (
|
3 |
Code,
|
4 |
Prompt,
|
5 |
AppType,
|
|
|
31 |
class Agent:
|
32 |
def __init__(self, prompts: Dict[str, any]):
|
33 |
self.prompts = prompts
|
34 |
+
self.client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
|
|
|
|
35 |
|
36 |
def process(self, user_input: str) -> str:
|
37 |
+
""" Processes the user's input and generates code. """
|
|
|
|
|
|
|
38 |
# Parse the user's input
|
39 |
app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial = self.parse_input(user_input)
|
40 |
|
|
|
55 |
return f"Code: {code}\nFiles: {files}"
|
56 |
|
57 |
def parse_input(self, user_input: str) -> tuple:
|
58 |
+
""" Parses the user's input and extracts the relevant information. """
|
|
|
|
|
|
|
59 |
# Extract the app type
|
60 |
app_type = self.extract_app_type(user_input)
|
|
|
61 |
# Extract the app name
|
62 |
app_name = self.extract_app_name(user_input)
|
|
|
63 |
# Extract the app description
|
64 |
app_description = self.extract_app_description(user_input)
|
|
|
65 |
# Extract the app features
|
66 |
app_features = self.extract_app_features(user_input)
|
|
|
67 |
# Extract the app dependencies
|
68 |
app_dependencies = self.extract_app_dependencies(user_input)
|
|
|
69 |
# Extract the app space
|
70 |
app_space = self.extract_app_space(user_input)
|
|
|
71 |
# Extract the app tutorial
|
72 |
app_tutorial = self.extract_app_tutorial(user_input)
|
73 |
|
74 |
return app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial
|
75 |
|
76 |
def extract_app_type(self, user_input: str) -> AppType:
|
77 |
+
""" Extracts the app type from the user's input. """
|
|
|
|
|
|
|
78 |
# Check if the user specified a specific app type
|
79 |
if "web app" in user_input:
|
80 |
return AppType.WEB_APP
|
|
|
84 |
return AppType.STREAMLIT_APP
|
85 |
elif "react app" in user_input:
|
86 |
return AppType.REACT_APP
|
|
|
87 |
# Otherwise, assume the user wants a web app
|
88 |
return AppType.WEB_APP
|
89 |
|
90 |
def extract_app_name(self, user_input: str) -> str:
|
91 |
+
""" Extracts the app name from the user's input. """
|
|
|
|
|
|
|
92 |
# Find the substring "app name is:"
|
93 |
start_index = user_input.find("app name is:") + len("app name is:")
|
|
|
94 |
# Find the end of the app name
|
95 |
end_index = user_input.find(".", start_index)
|
|
|
96 |
# Extract the app name
|
97 |
app_name = user_input[start_index:end_index].strip()
|
|
|
98 |
return app_name
|
99 |
|
100 |
def extract_app_description(self, user_input: str) -> str:
|
101 |
+
""" Extracts the app description from the user's input. """
|
|
|
|
|
|
|
102 |
# Find the substring "app description is:"
|
103 |
start_index = user_input.find("app description is:") + len("app description is:")
|
|
|
104 |
# Find the end of the app description
|
105 |
end_index = user_input.find(".", start_index)
|
|
|
106 |
# Extract the app description
|
107 |
app_description = user_input[start_index:end_index].strip()
|
|
|
108 |
return app_description
|
109 |
|
110 |
def extract_app_features(self, user_input: str) -> List[str]:
|
111 |
+
""" Extracts the app features from the user's input. """
|
|
|
|
|
|
|
112 |
# Find the substring "app features are:"
|
113 |
start_index = user_input.find("app features are:") + len("app features are:")
|
|
|
114 |
# Find the end of the app features
|
115 |
end_index = user_input.find(".", start_index)
|
|
|
116 |
# Extract the app features
|
117 |
app_features_str = user_input[start_index:end_index].strip()
|
|
|
118 |
# Split the app features string into a list
|
119 |
app_features = app_features_str.split(", ")
|
|
|
120 |
return app_features
|
121 |
|
122 |
def extract_app_dependencies(self, user_input: str) -> List[str]:
|
123 |
+
""" Extracts the app dependencies from the user's input. """
|
|
|
|
|
|
|
124 |
# Find the substring "app dependencies are:"
|
125 |
start_index = user_input.find("app dependencies are:") + len("app dependencies are:")
|
|
|
126 |
# Find the end of the app dependencies
|
127 |
end_index = user_input.find(".", start_index)
|
|
|
128 |
# Extract the app dependencies
|
129 |
app_dependencies_str = user_input[start_index:end_index].strip()
|
|
|
130 |
# Split the app dependencies string into a list
|
131 |
app_dependencies = app_dependencies_str.split(", ")
|
|
|
132 |
return app_dependencies
|
133 |
|
134 |
def extract_app_space(self, user_input: str) -> Optional[Space]:
|
135 |
+
""" Extracts the app space from the user's input. """
|
|
|
|
|
|
|
136 |
# Find the substring "app space is:"
|
137 |
start_index = user_input.find("app space is:") + len("app space is:")
|
|
|
138 |
# Find the end of the app space
|
139 |
end_index = user_input.find(".", start_index)
|
|
|
140 |
# Extract the app space
|
141 |
app_space_str = user_input[start_index:end_index].strip()
|
|
|
142 |
# Create a Space object
|
143 |
app_space = Space(space=app_space_str)
|
|
|
144 |
return app_space
|
145 |
|
146 |
def extract_app_tutorial(self, user_input: str) -> Optional[Tutorial]:
|
147 |
+
""" Extracts the app tutorial from the user's input. """
|
|
|
|
|
|
|
148 |
# Find the substring "app tutorial is:"
|
149 |
start_index = user_input.find("app tutorial is:") + len("app tutorial is:")
|
|
|
150 |
# Find the end of the app tutorial
|
151 |
end_index = user_input.find(".", start_index)
|
|
|
152 |
# Extract the app tutorial
|
153 |
app_tutorial_str = user_input[start_index:end_index].strip()
|
|
|
154 |
# Create a Tutorial object
|
155 |
app_tutorial = Tutorial(tutorial=app_tutorial_str)
|
|
|
156 |
return app_tutorial
|
157 |
|
158 |
def generate_code(self, prompt: Prompt) -> Code:
|
159 |
+
""" Generates code using the Llama model. """
|
|
|
|
|
|
|
160 |
# Send the prompt to the Llama model
|
161 |
response = self.client(prompt.prompt)
|
|
|
162 |
# Extract the generated code
|
163 |
code = response["generated_text"]
|
164 |
code = code.replace("```", "")
|
165 |
code = code.replace("```", "")
|
|
|
166 |
# Create a Code object
|
167 |
code = Code(code=code, language="python")
|
|
|
168 |
return code
|
169 |
|
170 |
def generate_files(self, app_type: AppType, app_name: str, app_description: str, app_features: List[str], app_dependencies: List[str], app_space: Optional[Space] = None, app_tutorial: Optional[Tutorial] = None) -> List[File]:
|
171 |
+
""" Generates files for the application. """
|
|
|
|
|
|
|
172 |
# Generate files based on the app type
|
173 |
files = self.prompts["generateFiles"](
|
174 |
app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial
|
175 |
)
|
|
|
176 |
return files
|
177 |
+
|
178 |
+
def main():
|
179 |
+
""" Main function for the application. """
|
180 |
+
# Create an agent
|
181 |
+
agent = Agent(
|
182 |
+
prompts={
|
183 |
+
"createLlamaPrompt": createLlamaPrompt,
|
184 |
+
"createSpace": createSpace,
|
185 |
+
"isPythonOrGradioAppPrompt": isPythonOrGradioAppPrompt,
|
186 |
+
"isReactAppPrompt": isReactAppPrompt,
|
187 |
+
"isStreamlitAppPrompt": isStreamlitAppPrompt,
|
188 |
+
"getWebApp": getWebApp,
|
189 |
+
"getGradioApp": getGradioApp,
|
190 |
+
"getReactApp": getReactApp,
|
191 |
+
"getStreamlitApp": getStreamlitApp,
|
192 |
+
"parseTutorial": parseTutorial,
|
193 |
+
"generateFiles": generateFiles,
|
194 |
+
}
|
195 |
+
)
|
196 |
+
|
197 |
+
# Get user input
|
198 |
+
user_input = input("Enter your request: ")
|
199 |
+
|
200 |
+
# Process the user's input
|
201 |
+
response = agent.process(user_input)
|
202 |
+
|
203 |
+
# Print the response
|
204 |
+
print(response)
|
205 |
+
|
206 |
+
if __name__ == "__main__":
|
207 |
+
main()
|