Delete app.py
Browse files
app.py
DELETED
|
@@ -1,204 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import io
|
| 4 |
-
import sys
|
| 5 |
-
import requests
|
| 6 |
-
import subprocess
|
| 7 |
-
from duckduckgo_search import DDGS
|
| 8 |
-
from bs4 import BeautifulSoup
|
| 9 |
-
|
| 10 |
-
# Gemini API Key -> Hugging Face Space ke Secrets mein 'GEMINI_API_KEY' naam se save karein
|
| 11 |
-
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 12 |
-
GEMINI_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GEMINI_API_KEY}"
|
| 13 |
-
|
| 14 |
-
# ==========================================
|
| 15 |
-
# 1. CORE TOOLS DEFINITIONS (Original Full Logic)
|
| 16 |
-
# ==========================================
|
| 17 |
-
|
| 18 |
-
def web_search(query: str) -> str:
|
| 19 |
-
"""Internet par live search karne ke liye."""
|
| 20 |
-
try:
|
| 21 |
-
with DDGS() as ddgs:
|
| 22 |
-
results = list(ddgs.text(query, max_results=3))
|
| 23 |
-
return json.dumps([{"title": r['title'], "snippet": r['body'], "link": r['href']} for r in results])
|
| 24 |
-
except Exception as e:
|
| 25 |
-
return f"Search failed: {str(e)}"
|
| 26 |
-
|
| 27 |
-
def read_webpage(url: str) -> str:
|
| 28 |
-
"""Kisi bhi URL ka text content padhne ke liye."""
|
| 29 |
-
try:
|
| 30 |
-
resp = requests.get(url, timeout=10, headers={"User-Agent": "Mozilla/5.0"})
|
| 31 |
-
soup = BeautifulSoup(resp.text, 'html.parser')
|
| 32 |
-
text = ' '.join(soup.stripped_strings)[:3000]
|
| 33 |
-
return text
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return f"Could not read webpage: {str(e)}"
|
| 36 |
-
|
| 37 |
-
def calculator(expression: str) -> str:
|
| 38 |
-
"""Complex maths calculations solve karne ke liye."""
|
| 39 |
-
try:
|
| 40 |
-
allowed_chars = "0123456789+-*/(). "
|
| 41 |
-
if all(c in allowed_chars for c in expression):
|
| 42 |
-
return str(eval(expression, {"__builtins__": {}}, {}))
|
| 43 |
-
return "Error: Invalid characters in math expression."
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return f"Math error: {str(e)}"
|
| 46 |
-
|
| 47 |
-
def python_interpreter(code: str) -> str:
|
| 48 |
-
"""Python code run karke logic execute karne ke liye (Sandbox)."""
|
| 49 |
-
old_stdout = sys.stdout
|
| 50 |
-
redirected_output = sys.stdout = io.StringIO()
|
| 51 |
-
try:
|
| 52 |
-
exec(code, {"__builtins__": __builtins__}, {})
|
| 53 |
-
sys.stdout = old_stdout
|
| 54 |
-
return redirected_output.getvalue() or "Code executed successfully with no output."
|
| 55 |
-
except Exception as e:
|
| 56 |
-
sys.stdout = old_stdout
|
| 57 |
-
return f"Execution Error: {str(e)}"
|
| 58 |
-
|
| 59 |
-
def run_terminal_command(command: str) -> str:
|
| 60 |
-
"""Terminal commands trigger karne ke liye (Gradle compile, APK setup, etc.)"""
|
| 61 |
-
try:
|
| 62 |
-
result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=300)
|
| 63 |
-
output = f"STDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}"
|
| 64 |
-
return output if output.strip() else "Command executed successfully with no logs."
|
| 65 |
-
except Exception as e:
|
| 66 |
-
return f"Terminal Error: {str(e)}"
|
| 67 |
-
|
| 68 |
-
def setup_android_environment() -> str:
|
| 69 |
-
"""Server par OpenJDK 17 aur Android SDK commands setup karne ke liye."""
|
| 70 |
-
commands = [
|
| 71 |
-
"sudo apt-get update -y",
|
| 72 |
-
"sudo apt-get install -y openjdk-17-jdk wget unzip",
|
| 73 |
-
"mkdir -p $HOME/android-sdk/cmdline-tools",
|
| 74 |
-
"cd $HOME/android-sdk/cmdline-tools && wget -q https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -O tools.zip",
|
| 75 |
-
"cd $HOME/android-sdk/cmdline-tools && unzip -q tools.zip && mv cmdline-tools latest || true",
|
| 76 |
-
"echo 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64' >> $HOME/.bashrc",
|
| 77 |
-
"echo 'export ANDROID_HOME=$HOME/android-sdk' >> $HOME/.bashrc",
|
| 78 |
-
"echo 'export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools' >> $HOME/.bashrc"
|
| 79 |
-
]
|
| 80 |
-
full_cmd = " && ".join(commands)
|
| 81 |
-
return run_terminal_command(full_cmd)
|
| 82 |
-
|
| 83 |
-
# ==========================================
|
| 84 |
-
# 2. LLM TOOL SCHEMA (JSON Format for Gemini Flash)
|
| 85 |
-
# ==========================================
|
| 86 |
-
|
| 87 |
-
GEMINI_TOOLS = [{
|
| 88 |
-
"function_declarations": [
|
| 89 |
-
{
|
| 90 |
-
"name": "web_search",
|
| 91 |
-
"description": "Use this tool to search the internet for current events, news, or general info.",
|
| 92 |
-
"parameters": {
|
| 93 |
-
"type": "OBJECT",
|
| 94 |
-
"properties": {"query": {"type": "STRING", "description": "The search query"}},
|
| 95 |
-
"required": ["query"]
|
| 96 |
-
}
|
| 97 |
-
},
|
| 98 |
-
{
|
| 99 |
-
"name": "read_webpage",
|
| 100 |
-
"description": "Extract raw text content from a given website URL.",
|
| 101 |
-
"parameters": {
|
| 102 |
-
"type": "OBJECT",
|
| 103 |
-
"properties": {"url": {"type": "STRING", "description": "The full web URL"}},
|
| 104 |
-
"required": ["url"]
|
| 105 |
-
}
|
| 106 |
-
},
|
| 107 |
-
{
|
| 108 |
-
"name": "calculator",
|
| 109 |
-
"description": "Evaluate mathematical expressions. Input should only contain numbers and basic operators.",
|
| 110 |
-
"parameters": {
|
| 111 |
-
"type": "OBJECT",
|
| 112 |
-
"properties": {"expression": {"type": "STRING", "description": "The math expression"}},
|
| 113 |
-
"required": ["expression"]
|
| 114 |
-
}
|
| 115 |
-
},
|
| 116 |
-
{
|
| 117 |
-
"name": "python_interpreter",
|
| 118 |
-
"description": "Execute Python code to solve complex logical problems, data manipulation, or algorithms.",
|
| 119 |
-
"parameters": {
|
| 120 |
-
"type": "OBJECT",
|
| 121 |
-
"properties": {"code": {"type": "STRING", "description": "Clean Python code block"}},
|
| 122 |
-
"required": ["code"]
|
| 123 |
-
}
|
| 124 |
-
},
|
| 125 |
-
{
|
| 126 |
-
"name": "run_terminal_command",
|
| 127 |
-
"description": "Execute terminal workflow commands directly inside workspace for APK generation.",
|
| 128 |
-
"parameters": {
|
| 129 |
-
"type": "OBJECT",
|
| 130 |
-
"properties": {"command": {"type": "STRING", "description": "The shell command string"}},
|
| 131 |
-
"required": ["command"]
|
| 132 |
-
}
|
| 133 |
-
},
|
| 134 |
-
{
|
| 135 |
-
"name": "setup_android_environment",
|
| 136 |
-
"description": "Run installation scripts for OpenJDK 17 and Android core SDK commandline modules.",
|
| 137 |
-
"parameters": {"type": "OBJECT", "properties": {}}
|
| 138 |
-
}
|
| 139 |
-
]
|
| 140 |
-
}]
|
| 141 |
-
|
| 142 |
-
def execute_tool(name, args):
|
| 143 |
-
if name == "web_search": return web_search(args.get("query"))
|
| 144 |
-
if name == "read_webpage": return read_webpage(args.get("url"))
|
| 145 |
-
if name == "calculator": return calculator(args.get("expression"))
|
| 146 |
-
if name == "python_interpreter": return python_interpreter(args.get("code"))
|
| 147 |
-
if name == "run_terminal_command": return run_terminal_command(args.get("command"))
|
| 148 |
-
if name == "setup_android_environment": return setup_android_environment()
|
| 149 |
-
return "Unknown tool"
|
| 150 |
-
|
| 151 |
-
# ==========================================
|
| 152 |
-
# 3. AGENT CORE LOOP (Gemini Execution Flow)
|
| 153 |
-
# ==========================================
|
| 154 |
-
|
| 155 |
-
def run_agent_loop(message, history=[]):
|
| 156 |
-
contents = []
|
| 157 |
-
for user, bot in history:
|
| 158 |
-
contents.append({"role": "user", "parts": [{"text": user}]})
|
| 159 |
-
if bot: contents.append({"role": "model", "parts": [{"text": bot}]})
|
| 160 |
-
contents.append({"role": "user", "parts": [{"text": message}]})
|
| 161 |
-
|
| 162 |
-
payload = {
|
| 163 |
-
"contents": contents,
|
| 164 |
-
"tools": GEMINI_TOOLS,
|
| 165 |
-
"systemInstruction": {
|
| 166 |
-
"parts": [{"text": "You are a master developer agent inside the environment. You can execute tools instantly to compile APK paths."}]
|
| 167 |
-
}
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
try:
|
| 171 |
-
headers = {"Content-Type": "application/json"}
|
| 172 |
-
response = requests.post(GEMINI_URL, headers=headers, json=payload).json()
|
| 173 |
-
part = response["candidates"][0]["content"]["parts"][0]
|
| 174 |
-
|
| 175 |
-
if "functionCall" in part:
|
| 176 |
-
call = part["functionCall"]
|
| 177 |
-
func_name = call["name"]
|
| 178 |
-
func_args = call.get("args", {})
|
| 179 |
-
|
| 180 |
-
tool_output = execute_tool(func_name, func_args)
|
| 181 |
-
|
| 182 |
-
contents.append({"role": "model", "parts": [part]})
|
| 183 |
-
contents.append({
|
| 184 |
-
"role": "user",
|
| 185 |
-
"parts": [{
|
| 186 |
-
"functionResponse": {
|
| 187 |
-
"name": func_name,
|
| 188 |
-
"response": {"output": tool_output}
|
| 189 |
-
}
|
| 190 |
-
}]
|
| 191 |
-
})
|
| 192 |
-
|
| 193 |
-
final_payload = {"contents": contents, "tools": GEMINI_TOOLS}
|
| 194 |
-
final_resp = requests.post(GEMINI_URL, headers=headers, json=final_payload).json()
|
| 195 |
-
return final_resp["candidates"][0]["content"]["parts"][0]["text"]
|
| 196 |
-
|
| 197 |
-
return part["text"]
|
| 198 |
-
except Exception as e:
|
| 199 |
-
return f"Agent Logic Pipeline Error: {str(e)}"
|
| 200 |
-
|
| 201 |
-
if __name__ == "__main__":
|
| 202 |
-
print("Starting Antigravity Web Module on main port 7860...")
|
| 203 |
-
# Pure Python antigravity tool integration redirection
|
| 204 |
-
import antigravity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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