Chand11 commited on
Commit
6eeda89
Β·
verified Β·
1 Parent(s): 73cdf16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
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="AIzaSyAdvERSmk7q02T2pLNPoERqDD-QPuW4RTs")
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
- print("\nπŸ“Š Execution Output:\n", output)
43
-
44
- # If no useful output, refine recursively
45
- if "Traceback" in output or output.strip() == "":
46
- if depth < 3:
47
- print("\n⚑ Refining task...")
48
- return recursive_ai_executor(f"Fix the error: {output}", depth+1)
49
- else:
50
- print("\n❌ Stopping after 3 attempts.")
51
- return output
52
-
53
-
54
- # πŸš€ Interactive loop
55
- print("πŸ€– Recursive AI Executor (type 'exit' to quit)\n")
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!"}