Chand11 commited on
Commit
b98640a
Β·
verified Β·
1 Parent(s): d00321e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -53
app.py CHANGED
@@ -1,57 +1,46 @@
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="AIzaSyAdvERSmk7q02T2pLNPoERqDD-QPuW4RTs")
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)
21
- if code_match:
22
- return code_match[0]
23
- return response_text.strip()
24
 
25
- def run_code(code):
26
- """Executes code and returns output or error."""
27
  try:
28
- result = subprocess.run(
29
- ["python3", "-c", code],
30
- capture_output=True,
31
- text=True,
32
- timeout=10
33
- )
34
- return result.stdout if result.stdout else result.stderr
35
  except Exception as e:
36
- return str(e)
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!"}
 
1
  from fastapi import FastAPI
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ import gradio as gr
4
+ import uvicorn
5
+ from executor import recursive_executor
6
+
7
+ # Create FastAPI app
8
+ app = FastAPI(title="Recursive AI Executor")
9
+
10
+ # Enable CORS
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+
19
+ # Root endpoint
20
+ @app.get("/")
21
+ def root():
22
+ return {"message": "πŸš€ Recursive AI Executor is running on Hugging Face!"}
 
23
 
24
+ # Run recursive executor for Gradio
25
+ def run_executor(task: str):
26
  try:
27
+ result = recursive_executor(task)
28
+ return result
 
 
 
 
 
29
  except Exception as e:
30
+ return f"❌ Error: {e}"
31
+
32
+ # Build Gradio UI
33
+ demo = gr.Interface(
34
+ fn=run_executor,
35
+ inputs=gr.Textbox(label="Enter your task", placeholder="e.g. Write Python code for Fibonacci"),
36
+ outputs=gr.Textbox(label="Result"),
37
+ title="Recursive AI Executor",
38
+ description="Enter a task and let the AI recursively break it down and solve it."
39
+ )
40
+
41
+ # Mount Gradio app inside FastAPI
42
+ app = gr.mount_gradio_app(app, demo, path="/app")
43
+
44
+ # Run server
45
+ if __name__ == "__main__":
46
+ uvicorn.run(app, host="0.0.0.0", port=7860)