Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from tools.arabic_generator import ArabicTextGenerator
|
| 3 |
from tools.quran_search import QuranSearchEngine
|
|
|
|
| 4 |
|
|
|
|
| 5 |
text_gen = ArabicTextGenerator()
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Tab 2: Quran Surah Viewer (GUARANTEED TO WORK)
|
| 23 |
with gr.Tab("📖 القرآن الكريم"):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
inputs=
|
| 35 |
-
outputs=
|
| 36 |
)
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from tools.arabic_generator import ArabicTextGenerator
|
| 3 |
from tools.quran_search import QuranSearchEngine
|
| 4 |
+
from tools.tool_agent import ToolCallingAgent # <-- New import
|
| 5 |
|
| 6 |
+
# Initialize all models
|
| 7 |
text_gen = ArabicTextGenerator()
|
| 8 |
+
quran_searcher = QuranSearchEngine()
|
| 9 |
+
tool_agent = ToolCallingAgent() # <-- New agent
|
| 10 |
|
| 11 |
+
# Define tools for the agent
|
| 12 |
+
TOOLS = [
|
| 13 |
+
{
|
| 14 |
+
"name": "search_quran",
|
| 15 |
+
"description": "Search the Quran for verses",
|
| 16 |
+
"parameters": {
|
| 17 |
+
"query": {"type": "string"},
|
| 18 |
+
"max_results": {"type": "integer"}
|
| 19 |
+
}
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"name": "generate_text",
|
| 23 |
+
"description": "Generate Arabic text",
|
| 24 |
+
"parameters": {
|
| 25 |
+
"prompt": {"type": "string"},
|
| 26 |
+
"length": {"type": "integer"}
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
def execute_tool_call(tool_call):
|
| 32 |
+
"""Execute the tool and return results"""
|
| 33 |
+
if tool_call["tool_name"] == "search_quran":
|
| 34 |
+
return quran_searcher.search(
|
| 35 |
+
tool_call["parameters"]["query"],
|
| 36 |
+
tool_call["parameters"]["max_results"]
|
| 37 |
)
|
| 38 |
+
elif tool_call["tool_name"] == "generate_text":
|
| 39 |
+
return text_gen.generate(
|
| 40 |
+
tool_call["parameters"]["prompt"],
|
| 41 |
+
tool_call["parameters"]["length"]
|
| 42 |
+
)
|
| 43 |
+
return "Tool not found"
|
| 44 |
+
|
| 45 |
+
with gr.Blocks(title="Advanced Arabic Tools") as app:
|
| 46 |
+
# Existing tabs remain unchanged
|
| 47 |
+
with gr.Tab("🖊️ مولد النصوص"):
|
| 48 |
+
# ... (keep your existing Arabic generator UI)
|
| 49 |
|
|
|
|
| 50 |
with gr.Tab("📖 القرآن الكريم"):
|
| 51 |
+
# ... (keep your existing Quran search UI)
|
| 52 |
+
|
| 53 |
+
# New Tool Calling Agent tab
|
| 54 |
+
with gr.Tab("🛠️ الوكيل الذكي"):
|
| 55 |
+
tool_input = gr.Textbox(label="أدخل طلبك")
|
| 56 |
+
tool_output = gr.JSON(label="استجابة الأداة")
|
| 57 |
+
tool_btn = gr.Button("تنفيذ")
|
| 58 |
+
|
| 59 |
+
def run_tool_agent(query):
|
| 60 |
+
tool_call = tool_agent.generate(query, TOOLS)
|
| 61 |
+
if "error" in tool_call:
|
| 62 |
+
return tool_call
|
| 63 |
+
return execute_tool_call(tool_call)
|
| 64 |
|
| 65 |
+
tool_btn.click(
|
| 66 |
+
run_tool_agent,
|
| 67 |
+
inputs=tool_input,
|
| 68 |
+
outputs=tool_output
|
| 69 |
)
|
| 70 |
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
app.launch()
|