Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -46,18 +46,12 @@ def create_space(repo_name, sdk, profile, token):
|
|
46 |
return rid, log, iframe
|
47 |
|
48 |
def write_file(path, content, repo_id, profile, token):
|
49 |
-
"""
|
50 |
-
Write or overwrite any file in the Space.
|
51 |
-
The LLM can call this for app.py, requirements.txt, README.md, etc.
|
52 |
-
"""
|
53 |
if not (profile and token):
|
54 |
return "⚠️ Please log in first."
|
55 |
if not repo_id:
|
56 |
return "⚠️ Please create a Space first."
|
57 |
-
# Save locally
|
58 |
with open(path, "w") as f:
|
59 |
f.write(content)
|
60 |
-
# Upload to HF Space
|
61 |
upload_file(path, path, repo_id,
|
62 |
token=token.token, repo_type="space")
|
63 |
return f"✅ Wrote and uploaded `{path}`"
|
@@ -107,49 +101,23 @@ def get_container_logs(repo_id, profile, token):
|
|
107 |
func_decls = [
|
108 |
{
|
109 |
"name":"create_space","description":"Create/get a HF Space",
|
110 |
-
"parameters":{
|
111 |
-
"type":"object",
|
112 |
-
"properties":{
|
113 |
-
"repo_name":{"type":"string"},
|
114 |
-
"sdk":{"type":"string","enum":["gradio","streamlit"]}
|
115 |
-
},
|
116 |
-
"required":["repo_name","sdk"]
|
117 |
-
}
|
118 |
},
|
119 |
{
|
120 |
"name":"write_file","description":"Write/overwrite a file in the Space",
|
121 |
-
"parameters":{
|
122 |
-
"type":"object",
|
123 |
-
"properties":{
|
124 |
-
"path":{"type":"string","description":"File path, e.g. app.py or requirements.txt"},
|
125 |
-
"content":{"type":"string","description":"The full text content to write to the file."}
|
126 |
-
},
|
127 |
-
"required":["path","content"]
|
128 |
-
}
|
129 |
},
|
130 |
{
|
131 |
"name":"list_files","description":"List files in the Space",
|
132 |
-
"parameters":{
|
133 |
-
"type":"object",
|
134 |
-
"properties":{"repo_id":{"type":"string"}},
|
135 |
-
"required":["repo_id"]
|
136 |
-
}
|
137 |
},
|
138 |
{
|
139 |
"name":"get_build_logs","description":"Fetch build logs",
|
140 |
-
"parameters":{
|
141 |
-
"type":"object",
|
142 |
-
"properties":{"repo_id":{"type":"string"}},
|
143 |
-
"required":["repo_id"]
|
144 |
-
}
|
145 |
},
|
146 |
{
|
147 |
"name":"get_run_logs","description":"Fetch run logs",
|
148 |
-
"parameters":{
|
149 |
-
"type":"object",
|
150 |
-
"properties":{"repo_id":{"type":"string"}},
|
151 |
-
"required":["repo_id"]
|
152 |
-
}
|
153 |
}
|
154 |
]
|
155 |
|
@@ -176,7 +144,7 @@ def process_message(profile, token, user_msg,
|
|
176 |
session["repo_id"] = None
|
177 |
session["messages"]= []
|
178 |
|
179 |
-
#
|
180 |
if session["repo_id"] is None:
|
181 |
m = re.search(r"call (?:it )?([A-Za-z0-9_-]+)", user_msg, re.IGNORECASE)
|
182 |
if m:
|
@@ -192,8 +160,10 @@ def process_message(profile, token, user_msg,
|
|
192 |
"role":"assistant",
|
193 |
"content":f"✅ Created Space `{name}`. {log}"
|
194 |
})
|
195 |
-
return (
|
196 |
-
|
|
|
|
|
197 |
|
198 |
# Record user message
|
199 |
session["messages"].append({"role":"user","content":user_msg})
|
@@ -214,42 +184,42 @@ def process_message(profile, token, user_msg,
|
|
214 |
args["repo_name"], args["sdk"], profile, token
|
215 |
)
|
216 |
session["repo_id"] = rid
|
217 |
-
result = {"log":log,"iframe":iframe}
|
218 |
|
219 |
elif fn == "write_file":
|
220 |
status = write_file(
|
221 |
args["path"], args["content"],
|
222 |
session["repo_id"], profile, token
|
223 |
)
|
224 |
-
result = {"status":status}
|
225 |
|
226 |
elif fn == "list_files":
|
227 |
fl = list_repo_files(
|
228 |
session["repo_id"], token=token.token, repo_type="space"
|
229 |
)
|
230 |
-
result = {"files":"\n".join(fl)}
|
231 |
|
232 |
elif fn == "get_build_logs":
|
233 |
-
result = {"log":get_build_logs(
|
234 |
session["repo_id"], profile, token
|
235 |
)}
|
236 |
|
237 |
elif fn == "get_run_logs":
|
238 |
-
result = {"log":get_container_logs(
|
239 |
session["repo_id"], profile, token
|
240 |
)}
|
241 |
|
242 |
else:
|
243 |
-
result = {"log":f"⚠️ Unknown function {fn}"}
|
244 |
|
245 |
-
#
|
246 |
session["chat"].send_message(
|
247 |
types.Content(
|
248 |
role="function",
|
249 |
parts=[ types.Part(
|
250 |
function_call=part.function_call,
|
251 |
-
function_response=
|
252 |
-
)]
|
253 |
)
|
254 |
)
|
255 |
assistant_text = session["chat"].get_history()[-1].parts[0].text
|
|
|
46 |
return rid, log, iframe
|
47 |
|
48 |
def write_file(path, content, repo_id, profile, token):
|
|
|
|
|
|
|
|
|
49 |
if not (profile and token):
|
50 |
return "⚠️ Please log in first."
|
51 |
if not repo_id:
|
52 |
return "⚠️ Please create a Space first."
|
|
|
53 |
with open(path, "w") as f:
|
54 |
f.write(content)
|
|
|
55 |
upload_file(path, path, repo_id,
|
56 |
token=token.token, repo_type="space")
|
57 |
return f"✅ Wrote and uploaded `{path}`"
|
|
|
101 |
func_decls = [
|
102 |
{
|
103 |
"name":"create_space","description":"Create/get a HF Space",
|
104 |
+
"parameters":{...}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
},
|
106 |
{
|
107 |
"name":"write_file","description":"Write/overwrite a file in the Space",
|
108 |
+
"parameters":{...}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
},
|
110 |
{
|
111 |
"name":"list_files","description":"List files in the Space",
|
112 |
+
"parameters":{...}
|
|
|
|
|
|
|
|
|
113 |
},
|
114 |
{
|
115 |
"name":"get_build_logs","description":"Fetch build logs",
|
116 |
+
"parameters":{...}
|
|
|
|
|
|
|
|
|
117 |
},
|
118 |
{
|
119 |
"name":"get_run_logs","description":"Fetch run logs",
|
120 |
+
"parameters":{...}
|
|
|
|
|
|
|
|
|
121 |
}
|
122 |
]
|
123 |
|
|
|
144 |
session["repo_id"] = None
|
145 |
session["messages"]= []
|
146 |
|
147 |
+
# Quick create via “call it NAME”
|
148 |
if session["repo_id"] is None:
|
149 |
m = re.search(r"call (?:it )?([A-Za-z0-9_-]+)", user_msg, re.IGNORECASE)
|
150 |
if m:
|
|
|
160 |
"role":"assistant",
|
161 |
"content":f"✅ Created Space `{name}`. {log}"
|
162 |
})
|
163 |
+
return (
|
164 |
+
session["messages"],
|
165 |
+
iframe, log, "", session
|
166 |
+
)
|
167 |
|
168 |
# Record user message
|
169 |
session["messages"].append({"role":"user","content":user_msg})
|
|
|
184 |
args["repo_name"], args["sdk"], profile, token
|
185 |
)
|
186 |
session["repo_id"] = rid
|
187 |
+
result = {"log": log, "iframe": iframe}
|
188 |
|
189 |
elif fn == "write_file":
|
190 |
status = write_file(
|
191 |
args["path"], args["content"],
|
192 |
session["repo_id"], profile, token
|
193 |
)
|
194 |
+
result = {"status": status}
|
195 |
|
196 |
elif fn == "list_files":
|
197 |
fl = list_repo_files(
|
198 |
session["repo_id"], token=token.token, repo_type="space"
|
199 |
)
|
200 |
+
result = {"files": "\n".join(fl)}
|
201 |
|
202 |
elif fn == "get_build_logs":
|
203 |
+
result = {"log": get_build_logs(
|
204 |
session["repo_id"], profile, token
|
205 |
)}
|
206 |
|
207 |
elif fn == "get_run_logs":
|
208 |
+
result = {"log": get_container_logs(
|
209 |
session["repo_id"], profile, token
|
210 |
)}
|
211 |
|
212 |
else:
|
213 |
+
result = {"log": f"⚠️ Unknown function {fn}"}
|
214 |
|
215 |
+
# Pass the raw dict so Pydantic can validate it
|
216 |
session["chat"].send_message(
|
217 |
types.Content(
|
218 |
role="function",
|
219 |
parts=[ types.Part(
|
220 |
function_call=part.function_call,
|
221 |
+
function_response=result
|
222 |
+
) ]
|
223 |
)
|
224 |
)
|
225 |
assistant_text = session["chat"].get_history()[-1].parts[0].text
|