Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ from google.genai import types
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# Gemini Client Initialize karein (Hugging Face Secrets se key lega)
|
| 11 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 12 |
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 13 |
|
|
@@ -15,10 +14,22 @@ class ChatRequest(BaseModel):
|
|
| 15 |
message: str
|
| 16 |
|
| 17 |
def execute_in_sandbox(command: str):
|
| 18 |
-
"""Ye function terminal commands ko sandbox ke andar safe tareeke se chalata hai"""
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
return {"error": str(e)}
|
| 24 |
|
|
@@ -26,30 +37,26 @@ def execute_in_sandbox(command: str):
|
|
| 26 |
async def agent_chat(request: ChatRequest):
|
| 27 |
user_prompt = request.message
|
| 28 |
|
| 29 |
-
#
|
| 30 |
system_instruction = (
|
| 31 |
-
"You are an AI Agent Builder
|
| 32 |
-
"You
|
| 33 |
-
"
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
# Gemini 2.5 Flash ya Pro use karein projects ke liye
|
| 38 |
response = client.models.generate_content(
|
| 39 |
model='gemini-2.5-flash',
|
| 40 |
contents=user_prompt,
|
| 41 |
-
config=types.GenerateContentConfig(
|
| 42 |
-
system_instruction=system_instruction
|
| 43 |
-
)
|
| 44 |
)
|
| 45 |
|
| 46 |
ai_response = response.text
|
| 47 |
|
| 48 |
-
# Agar AI ne koi command execute karne ko kaha hai (Basic automation logic)
|
| 49 |
if "```bash" in ai_response:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
# Command sandbox me chalayein
|
| 53 |
exec_result = execute_in_sandbox(command)
|
| 54 |
return {"agent_response": ai_response, "execution_result": exec_result}
|
| 55 |
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 10 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 11 |
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 12 |
|
|
|
|
| 14 |
message: str
|
| 15 |
|
| 16 |
def execute_in_sandbox(command: str):
|
|
|
|
| 17 |
try:
|
| 18 |
+
# Commands hamesha writeable directory (/tmp) me run ho, isliye cd /tmp lagaya hai
|
| 19 |
+
full_command = f"cd /tmp && {command}"
|
| 20 |
+
|
| 21 |
+
result = subprocess.run(
|
| 22 |
+
full_command,
|
| 23 |
+
shell=True,
|
| 24 |
+
capture_output=True,
|
| 25 |
+
text=True,
|
| 26 |
+
timeout=600 # 10 mins timeout taaki installation aaram se ho sake
|
| 27 |
+
)
|
| 28 |
+
return {
|
| 29 |
+
"stdout": result.stdout,
|
| 30 |
+
"stderr": result.stderr,
|
| 31 |
+
"exit_code": result.returncode
|
| 32 |
+
}
|
| 33 |
except Exception as e:
|
| 34 |
return {"error": str(e)}
|
| 35 |
|
|
|
|
| 37 |
async def agent_chat(request: ChatRequest):
|
| 38 |
user_prompt = request.message
|
| 39 |
|
| 40 |
+
# AI ko instruction do ki use jo chahiye wo khud bash command se install kare (e.g., flutter, sdk)
|
| 41 |
system_instruction = (
|
| 42 |
+
"You are an AI Agent Builder operating inside a limited Linux sandbox. "
|
| 43 |
+
"You have full permission to download and install necessary tools (like Flutter, Node.js, Android cmdline-tools) "
|
| 44 |
+
"using bash commands when required. Always perform your operations inside the `/tmp` directory because it has write permissions. "
|
| 45 |
+
"If you want to execute any tool or bash setup, return the exact script inside a triple backtick block starting with 'bash'."
|
| 46 |
)
|
| 47 |
|
| 48 |
try:
|
|
|
|
| 49 |
response = client.models.generate_content(
|
| 50 |
model='gemini-2.5-flash',
|
| 51 |
contents=user_prompt,
|
| 52 |
+
config=types.GenerateContentConfig(system_instruction=system_instruction)
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
ai_response = response.text
|
| 56 |
|
|
|
|
| 57 |
if "```bash" in ai_response:
|
| 58 |
+
command = ai_response.split("
|
| 59 |
+
```bash")[1].split("```")[0].strip()
|
|
|
|
| 60 |
exec_result = execute_in_sandbox(command)
|
| 61 |
return {"agent_response": ai_response, "execution_result": exec_result}
|
| 62 |
|