Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -231,52 +231,70 @@ return history, terminal_history + f"User: {message}\nAssistant: {response}"
|
|
| 231 |
else:
|
| 232 |
return history, terminal_history + f"User: {message}\nAssistant: I'm sorry, I couldn't generate a response. Please try again.\n"
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
--- Code Generation ---
|
| 235 |
def generate_python_code(app_name):
|
| 236 |
-
code = f"""import gradio as gr
|
| 237 |
-
Define your Gradio components here
|
| 238 |
-
with gr.Blocks() as {app_name}: """
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
code += f"""
|
| 241 |
-
{app_name}.launch() """
|
|
|
|
| 242 |
|
| 243 |
--- Hugging Face Deployment ---
|
| 244 |
def deploy_to_huggingface(app_name):
|
| 245 |
|
| 246 |
-
Generate Python code
|
| 247 |
-
code = generate_python_code(app_name)
|
| 248 |
|
| 249 |
-
Create requirements.txt
|
| 250 |
-
with open("requirements.txt", "w") as f:
|
| 251 |
-
f.write("gradio==3.32.0\n")
|
| 252 |
|
| 253 |
-
Create the app.py file
|
| 254 |
-
with open("app.py", "w") as f:
|
| 255 |
-
f.write(code)
|
| 256 |
|
| 257 |
-
Execute the deployment command
|
| 258 |
-
try:
|
| 259 |
-
subprocess.run(
|
| 260 |
-
["huggingface-cli", "repo", "create", "--type", "space", "--space_sdk", "gradio", app_name],
|
| 261 |
-
check=True
|
| 262 |
-
)
|
| 263 |
-
subprocess.run(
|
| 264 |
-
["git", "init"], cwd=f"./{app_name}
|
| 265 |
-
)
|
| 266 |
-
subprocess.run(
|
| 267 |
-
["git", "add", "."], cwd=f
|
| 268 |
-
)
|
| 269 |
-
subprocess.run(
|
| 270 |
-
['git', 'commit', '-m', '"Initial commit"'], cwd=f
|
| 271 |
-
)
|
| 272 |
-
subprocess.run(
|
| 273 |
-
["git", "push", "https://huggingface.co/spaces/" + app_name, "main"], cwd=f
|
| 274 |
-
)
|
| 275 |
-
return (
|
| 276 |
-
f"Successfully deployed to Hugging Face Spaces: https://huggingface.co/spaces/{app\_name}"
|
| 277 |
-
)
|
| 278 |
-
except Exception as e:
|
| 279 |
-
return f"Error deploying to Hugging Face Spaces: {e}"
|
| 280 |
|
| 281 |
--- Gradio Interface ---
|
| 282 |
with gr.Blocks() as iface:
|
|
|
|
| 231 |
else:
|
| 232 |
return history, terminal_history + f"User: {message}\nAssistant: I'm sorry, I couldn't generate a response. Please try again.\n"
|
| 233 |
|
| 234 |
+
--- Function to handle chat interaction ---
|
| 235 |
+
def run_chat(message, history):
|
| 236 |
+
global terminal_history
|
| 237 |
+
if message.startswith("!"):
|
| 238 |
+
command = message[1:]
|
| 239 |
+
terminal_history = run_terminal_command(command, history)
|
| 240 |
+
else:
|
| 241 |
+
# ... (Your regular chat response generation)
|
| 242 |
+
model_index = 0 # Select the model to use for chat response
|
| 243 |
+
response = get_nlp_response(message, model_index)
|
| 244 |
+
if response:
|
| 245 |
+
return history, terminal_history + f"""User: {message}\nAssistant: {response}"""
|
| 246 |
+
else:
|
| 247 |
+
return history, terminal_history + f"""User: {message}\nAssistant: I'm sorry, I couldn't generate a response. Please try again.\n"""
|
| 248 |
+
|
| 249 |
--- Code Generation ---
|
| 250 |
def generate_python_code(app_name):
|
| 251 |
+
code = f"""import gradio as gr
|
| 252 |
+
Define your Gradio components here
|
| 253 |
+
with gr.Blocks() as {app_name}: """
|
| 254 |
+
for component in app_state["components"]:
|
| 255 |
+
code += " " + Component(**component).render() + "\n"
|
| 256 |
|
| 257 |
+
code += f"""
|
| 258 |
+
{app_name}.launch() """
|
| 259 |
+
return code
|
| 260 |
|
| 261 |
--- Hugging Face Deployment ---
|
| 262 |
def deploy_to_huggingface(app_name):
|
| 263 |
|
| 264 |
+
# Generate Python code
|
| 265 |
+
code = generate_python_code(app_name)
|
| 266 |
|
| 267 |
+
# Create requirements.txt
|
| 268 |
+
with open("requirements.txt", "w") as f:
|
| 269 |
+
f.write("gradio==3.32.0\n")
|
| 270 |
|
| 271 |
+
# Create the app.py file
|
| 272 |
+
with open("app.py", "w") as f:
|
| 273 |
+
f.write(code)
|
| 274 |
|
| 275 |
+
# Execute the deployment command
|
| 276 |
+
try:
|
| 277 |
+
subprocess.run(
|
| 278 |
+
"""["huggingface-cli", "repo", "create", "--type", "space", "--space_sdk", "gradio", app_name]""",
|
| 279 |
+
check=True
|
| 280 |
+
)
|
| 281 |
+
subprocess.run(
|
| 282 |
+
"""["git", "init"]""", cwd=f"""./{app_name}""", check=True
|
| 283 |
+
)
|
| 284 |
+
subprocess.run(
|
| 285 |
+
"""["git", "add", "."]""", cwd=f"""./{app_name}""", check=True
|
| 286 |
+
)
|
| 287 |
+
subprocess.run(
|
| 288 |
+
"""['git', 'commit', '-m', '"Initial commit"']""", cwd=f"""./{app_name}""", check=True
|
| 289 |
+
)
|
| 290 |
+
subprocess.run(
|
| 291 |
+
"""["git", "push", "https://huggingface.co/spaces/" + app_name, "main"]""", cwd=f"""./{app_name}""", check=True
|
| 292 |
+
)
|
| 293 |
+
return (
|
| 294 |
+
f"""Successfully deployed to Hugging Face Spaces: https://huggingface.co/spaces/{app\_name}"""
|
| 295 |
+
)
|
| 296 |
+
except Exception as e:
|
| 297 |
+
return f"""Error deploying to Hugging Face Spaces: {e}"""
|
| 298 |
|
| 299 |
--- Gradio Interface ---
|
| 300 |
with gr.Blocks() as iface:
|