Delete app.py
Browse files
app.py
DELETED
|
@@ -1,182 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import io
|
| 4 |
-
import sys
|
| 5 |
-
import requests
|
| 6 |
-
import gradio as gr
|
| 7 |
-
from duckduckgo_search import DDGS
|
| 8 |
-
from bs4 import BeautifulSoup
|
| 9 |
-
|
| 10 |
-
# Hugging Face Token Settings
|
| 11 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 12 |
-
API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct/v1/chat/completions"
|
| 13 |
-
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 14 |
-
|
| 15 |
-
# Core Tools
|
| 16 |
-
def web_search(query: str) -> str:
|
| 17 |
-
try:
|
| 18 |
-
with DDGS() as ddgs:
|
| 19 |
-
results = list(ddgs.text(query, max_results=3))
|
| 20 |
-
return json.dumps([{"title": r['title'], "snippet": r['body'], "link": r['href']} for r in results])
|
| 21 |
-
except Exception as e:
|
| 22 |
-
return f"Search failed: {str(e)}"
|
| 23 |
-
|
| 24 |
-
def read_webpage(url: str) -> str:
|
| 25 |
-
try:
|
| 26 |
-
resp = requests.get(url, timeout=10, headers={"User-Agent": "Mozilla/5.0"})
|
| 27 |
-
soup = BeautifulSoup(resp.text, 'html.parser')
|
| 28 |
-
text = ' '.join(soup.stripped_strings)[:3000]
|
| 29 |
-
return text
|
| 30 |
-
except Exception as e:
|
| 31 |
-
return f"Could not read webpage: {str(e)}"
|
| 32 |
-
|
| 33 |
-
def calculator(expression: str) -> str:
|
| 34 |
-
try:
|
| 35 |
-
allowed_chars = "0123456789+-*/(). "
|
| 36 |
-
if all(c in allowed_chars for c in expression):
|
| 37 |
-
return str(eval(expression, {"__builtins__": {}}, {}))
|
| 38 |
-
return "Error: Invalid characters in math expression."
|
| 39 |
-
except Exception as e:
|
| 40 |
-
return f"Math error: {str(e)}"
|
| 41 |
-
|
| 42 |
-
def python_interpreter(code: str) -> str:
|
| 43 |
-
old_stdout = sys.stdout
|
| 44 |
-
redirected_output = sys.stdout = io.StringIO()
|
| 45 |
-
try:
|
| 46 |
-
exec(code, {"__builtins__": __builtins__}, {})
|
| 47 |
-
sys.stdout = old_stdout
|
| 48 |
-
return redirected_output.getvalue() or "Code executed successfully with no output."
|
| 49 |
-
except Exception as e:
|
| 50 |
-
sys.stdout = old_stdout
|
| 51 |
-
return f"Execution Error: {str(e)}"
|
| 52 |
-
|
| 53 |
-
TOOLS = [
|
| 54 |
-
{
|
| 55 |
-
"type": "function",
|
| 56 |
-
"function": {
|
| 57 |
-
"name": "web_search",
|
| 58 |
-
"description": "Use this tool to search the internet for current events, news, or general info.",
|
| 59 |
-
"parameters": {
|
| 60 |
-
"type": "object",
|
| 61 |
-
"properties": {"query": {"type": "string", "description": "The search query"}},
|
| 62 |
-
"required": ["query"]
|
| 63 |
-
}
|
| 64 |
-
}
|
| 65 |
-
},
|
| 66 |
-
{
|
| 67 |
-
"type": "function",
|
| 68 |
-
"function": {
|
| 69 |
-
"name": "read_webpage",
|
| 70 |
-
"description": "Extract raw text content from a given website URL.",
|
| 71 |
-
"parameters": {
|
| 72 |
-
"type": "object",
|
| 73 |
-
"properties": {"url": {"type": "string", "description": "The full web URL"}},
|
| 74 |
-
"required": ["url"]
|
| 75 |
-
}
|
| 76 |
-
}
|
| 77 |
-
},
|
| 78 |
-
{
|
| 79 |
-
"type": "function",
|
| 80 |
-
"function": {
|
| 81 |
-
"name": "calculator",
|
| 82 |
-
"description": "Evaluate mathematical expressions.",
|
| 83 |
-
"parameters": {
|
| 84 |
-
"type": "object",
|
| 85 |
-
"properties": {"expression": {"type": "string", "description": "The math expression"}},
|
| 86 |
-
"required": ["expression"]
|
| 87 |
-
}
|
| 88 |
-
}
|
| 89 |
-
},
|
| 90 |
-
{
|
| 91 |
-
"type": "function",
|
| 92 |
-
"function": {
|
| 93 |
-
"name": "python_interpreter",
|
| 94 |
-
"description": "Execute Python code to solve complex logical problems.",
|
| 95 |
-
"parameters": {
|
| 96 |
-
"type": "object",
|
| 97 |
-
"properties": {"code": {"type": "string", "description": "Clean Python code block"}},
|
| 98 |
-
"required": ["code"]
|
| 99 |
-
}
|
| 100 |
-
}
|
| 101 |
-
}
|
| 102 |
-
]
|
| 103 |
-
|
| 104 |
-
def execute_tool(name, args):
|
| 105 |
-
if name == "web_search": return web_search(args.get("query"))
|
| 106 |
-
if name == "read_webpage": return read_webpage(args.get("url"))
|
| 107 |
-
if name == "calculator": return calculator(args.get("expression"))
|
| 108 |
-
if name == "python_interpreter": return python_interpreter(args.get("code"))
|
| 109 |
-
return "Unknown tool"
|
| 110 |
-
|
| 111 |
-
def run_agent(message, history):
|
| 112 |
-
messages = [{"role": "system", "content": "You are a helpful AI Agent equipped with advanced tools."}]
|
| 113 |
-
for user, bot in history:
|
| 114 |
-
messages.append({"role": "user", "content": user})
|
| 115 |
-
if bot: messages.append({"role": "assistant", "content": bot})
|
| 116 |
-
messages.append({"role": "user", "content": message})
|
| 117 |
-
|
| 118 |
-
payload = {
|
| 119 |
-
"model": "Qwen/Qwen2.5-72B-Instruct",
|
| 120 |
-
"messages": messages,
|
| 121 |
-
"tools": TOOLS,
|
| 122 |
-
"tool_choice": "auto"
|
| 123 |
-
}
|
| 124 |
-
|
| 125 |
-
try:
|
| 126 |
-
response = requests.post(API_URL, headers=HEADERS, json=payload).json()
|
| 127 |
-
choice = response["choices"][0]["message"]
|
| 128 |
-
|
| 129 |
-
if choice.get("tool_calls"):
|
| 130 |
-
tool_call = choice["tool_calls"][0]
|
| 131 |
-
func_name = tool_call["function"]["name"]
|
| 132 |
-
func_args = json.loads(tool_call["function"]["arguments"])
|
| 133 |
-
|
| 134 |
-
tool_output = execute_tool(func_name, func_args)
|
| 135 |
-
|
| 136 |
-
messages.append(choice)
|
| 137 |
-
messages.append({
|
| 138 |
-
"role": "tool",
|
| 139 |
-
"name": func_name,
|
| 140 |
-
"content": tool_output,
|
| 141 |
-
"tool_call_id": tool_call.get("id", "call_1")
|
| 142 |
-
})
|
| 143 |
-
|
| 144 |
-
final_payload = {"model": "Qwen/Qwen2.5-72B-Instruct", "messages": messages}
|
| 145 |
-
final_response = requests.post(API_URL, headers=HEADERS, json=final_payload).json()
|
| 146 |
-
return final_response["choices"][0]["message"]["content"]
|
| 147 |
-
|
| 148 |
-
return choice["content"]
|
| 149 |
-
except Exception as e:
|
| 150 |
-
return f"API Error: Check HF_TOKEN. Details: {str(e)}"
|
| 151 |
-
|
| 152 |
-
def get_system_details():
|
| 153 |
-
try:
|
| 154 |
-
ip_res = requests.get("https://api.ipify.org?format=json", timeout=5).json()
|
| 155 |
-
current_ip = ip_res.get("ip", "Not Found")
|
| 156 |
-
except Exception:
|
| 157 |
-
current_ip = "Hidden/Dynamic (HF Space Proxy Active)"
|
| 158 |
-
|
| 159 |
-
current_user = os.environ.get("USER", "user (UID: 1000)")
|
| 160 |
-
password_status = "No Password (NOPASSWD Enabled in Sudoers)"
|
| 161 |
-
|
| 162 |
-
info_html = f"""
|
| 163 |
-
<div style="background-color: #1e1e2e; color: #cdd6f4; padding: 15px; border-radius: 10px; margin-bottom: 20px; border: 1px solid #f38ba8;">
|
| 164 |
-
<h3 style="color: #f38ba8; margin-top: 0;">📱 Mobile Remote Control Access Info:</h3>
|
| 165 |
-
<p><b>🌐 Container Public IP:</b> <code style="background: #313244; padding: 2px 6px; border-radius: 4px; color: #a6e3a1;">{current_ip}</code></p>
|
| 166 |
-
<p><b>👤 System Username:</b> <code style="background: #313244; padding: 2px 6px; border-radius: 4px; color: #a6e3a1;">{current_user}</code></p>
|
| 167 |
-
<p><b>🔑 Password Status:</b> <code style="background: #313244; padding: 2px 6px; border-radius: 4px; color: #a6e3a1;">{password_status}</code></p>
|
| 168 |
-
</div>
|
| 169 |
-
"""
|
| 170 |
-
return info_html
|
| 171 |
-
|
| 172 |
-
with gr.Blocks(theme="soft") as demo:
|
| 173 |
-
gr.Markdown("# 📦 LobeHub-Style Docker Agent")
|
| 174 |
-
gr.HTML(get_system_details())
|
| 175 |
-
gr.ChatInterface(
|
| 176 |
-
fn=run_agent,
|
| 177 |
-
description="Docker container backend ke sath chalne wala Agent: Search, Browser, Math aur Python Interpreter sab free!",
|
| 178 |
-
)
|
| 179 |
-
|
| 180 |
-
if __name__ == "__main__":
|
| 181 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|