Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
|
|
|
|
|
| 1 |
import google.generativeai as genai
|
| 2 |
import subprocess
|
| 3 |
import re
|
| 4 |
|
| 5 |
# π Configure Gemini API
|
| 6 |
-
genai.configure(api_key="
|
| 7 |
|
| 8 |
# Pick a model
|
| 9 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def extract_code(response_text):
|
| 12 |
"""Extracts Python code from Gemini's response (removes ``` fences)."""
|
| 13 |
code_match = re.findall(r"```(?:python)?\n(.*?)```", response_text, re.DOTALL)
|
|
@@ -30,32 +37,21 @@ def run_code(code):
|
|
| 30 |
|
| 31 |
def recursive_ai_executor(task, depth=1):
|
| 32 |
"""Recursive AI Executor with auto-run + visible output."""
|
| 33 |
-
print(f"\nπΉ Task (Level {depth}): {task}")
|
| 34 |
-
|
| 35 |
-
# Ask Gemini to generate code + usage
|
| 36 |
prompt = f"Write Python code to {task}. Always include a sample call (like print(...)) so output is shown."
|
| 37 |
response = model.generate_content(prompt)
|
| 38 |
code = extract_code(response.text)
|
| 39 |
-
|
| 40 |
-
print("\nπ Generated Code:\n", code)
|
| 41 |
output = run_code(code)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
return
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
while True:
|
| 57 |
-
user_task = input("Enter your Python task: ")
|
| 58 |
-
if user_task.lower() in ["exit", "quit"]:
|
| 59 |
-
print("π Exiting AI Executor.")
|
| 60 |
-
break
|
| 61 |
-
recursive_ai_executor(user_task)
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
import google.generativeai as genai
|
| 4 |
import subprocess
|
| 5 |
import re
|
| 6 |
|
| 7 |
# π Configure Gemini API
|
| 8 |
+
genai.configure(api_key="YOUR_API_KEY")
|
| 9 |
|
| 10 |
# Pick a model
|
| 11 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 12 |
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
class TaskRequest(BaseModel):
|
| 16 |
+
task: str
|
| 17 |
+
|
| 18 |
def extract_code(response_text):
|
| 19 |
"""Extracts Python code from Gemini's response (removes ``` fences)."""
|
| 20 |
code_match = re.findall(r"```(?:python)?\n(.*?)```", response_text, re.DOTALL)
|
|
|
|
| 37 |
|
| 38 |
def recursive_ai_executor(task, depth=1):
|
| 39 |
"""Recursive AI Executor with auto-run + visible output."""
|
|
|
|
|
|
|
|
|
|
| 40 |
prompt = f"Write Python code to {task}. Always include a sample call (like print(...)) so output is shown."
|
| 41 |
response = model.generate_content(prompt)
|
| 42 |
code = extract_code(response.text)
|
|
|
|
|
|
|
| 43 |
output = run_code(code)
|
| 44 |
+
|
| 45 |
+
# Retry if failure
|
| 46 |
+
if ("Traceback" in output or output.strip() == "") and depth < 3:
|
| 47 |
+
return recursive_ai_executor(f"Fix the error: {output}", depth+1)
|
| 48 |
+
|
| 49 |
+
return {"generated_code": code, "execution_output": output}
|
| 50 |
+
|
| 51 |
+
@app.post("/run-task")
|
| 52 |
+
def run_task(request: TaskRequest):
|
| 53 |
+
return recursive_ai_executor(request.task)
|
| 54 |
+
|
| 55 |
+
@app.get("/")
|
| 56 |
+
def home():
|
| 57 |
+
return {"message": "π Recursive AI Executor is running on Hugging Face!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|