Update app.py
Browse files
app.py
CHANGED
|
@@ -3,19 +3,18 @@ from tools.arabic_generator import ArabicTextGenerator
|
|
| 3 |
from tools.quran_search import QuranSearchEngine
|
| 4 |
from tools.tool_agent import ToolCallingAgent
|
| 5 |
|
| 6 |
-
# Initialize
|
| 7 |
text_gen = ArabicTextGenerator()
|
| 8 |
-
|
| 9 |
-
tool_agent = ToolCallingAgent()
|
| 10 |
|
| 11 |
-
# Define tools for the agent
|
| 12 |
TOOLS = [
|
| 13 |
{
|
| 14 |
"name": "search_quran",
|
| 15 |
-
"description": "Search
|
| 16 |
"parameters": {
|
| 17 |
"query": {"type": "string"},
|
| 18 |
-
"max_results": {"type": "integer"}
|
| 19 |
}
|
| 20 |
},
|
| 21 |
{
|
|
@@ -23,60 +22,50 @@ TOOLS = [
|
|
| 23 |
"description": "Generate Arabic text",
|
| 24 |
"parameters": {
|
| 25 |
"prompt": {"type": "string"},
|
| 26 |
-
"length": {"type": "integer"}
|
| 27 |
}
|
| 28 |
}
|
| 29 |
]
|
| 30 |
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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="
|
| 46 |
-
# Tab 1: Arabic
|
| 47 |
with gr.Tab("🖊️ مولد النصوص"):
|
| 48 |
-
|
| 49 |
-
length_slider = gr.Slider(50, 300, value=100, label="طول النص")
|
| 50 |
-
gen_btn = gr.Button("توليد")
|
| 51 |
-
text_output = gr.Textbox(label="النص المولد", lines=6)
|
| 52 |
-
|
| 53 |
-
gen_btn.click(
|
| 54 |
-
text_gen.generate,
|
| 55 |
-
inputs=[text_input, length_slider],
|
| 56 |
-
outputs=text_output
|
| 57 |
-
)
|
| 58 |
|
| 59 |
-
# Tab 2: Quran Search
|
| 60 |
with gr.Tab("📖 القرآن الكريم"):
|
| 61 |
-
|
| 62 |
-
top_k = gr.Slider(1, 10, value=5, label="عدد النتائج")
|
| 63 |
-
search_btn = gr.Button("ابحث")
|
| 64 |
-
search_output = gr.Textbox(label="النتائج", lines=10)
|
| 65 |
-
|
| 66 |
-
search_btn.click(
|
| 67 |
-
lambda q, k: format_quran_results(quran_searcher.search(q, k)),
|
| 68 |
-
inputs=[search_input, top_k],
|
| 69 |
-
outputs=search_output
|
| 70 |
-
)
|
| 71 |
|
| 72 |
-
# Tab 3: Tool
|
| 73 |
-
with gr.Tab("🛠️
|
| 74 |
-
tool_input = gr.Textbox(label="
|
| 75 |
-
tool_output = gr.
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
inputs=tool_input,
|
| 81 |
outputs=tool_output
|
| 82 |
)
|
|
|
|
| 3 |
from tools.quran_search import QuranSearchEngine
|
| 4 |
from tools.tool_agent import ToolCallingAgent
|
| 5 |
|
| 6 |
+
# Initialize models
|
| 7 |
text_gen = ArabicTextGenerator()
|
| 8 |
+
quran = QuranSearchEngine()
|
| 9 |
+
tool_agent = ToolCallingAgent() # CPU-only
|
| 10 |
|
|
|
|
| 11 |
TOOLS = [
|
| 12 |
{
|
| 13 |
"name": "search_quran",
|
| 14 |
+
"description": "Search Quran verses",
|
| 15 |
"parameters": {
|
| 16 |
"query": {"type": "string"},
|
| 17 |
+
"max_results": {"type": "integer", "max": 5}
|
| 18 |
}
|
| 19 |
},
|
| 20 |
{
|
|
|
|
| 22 |
"description": "Generate Arabic text",
|
| 23 |
"parameters": {
|
| 24 |
"prompt": {"type": "string"},
|
| 25 |
+
"length": {"type": "integer", "max": 150}
|
| 26 |
}
|
| 27 |
}
|
| 28 |
]
|
| 29 |
|
| 30 |
+
def format_output(result):
|
| 31 |
+
if isinstance(result, list): # Quran results
|
| 32 |
+
return "\n".join(f"{r['surah']} {r['ayah']}: {r['text']}" for r in result)
|
| 33 |
+
return str(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
with gr.Blocks(title="الأدوات العربية") as app:
|
| 36 |
+
# Tab 1: Existing Arabic Generator
|
| 37 |
with gr.Tab("🖊️ مولد النصوص"):
|
| 38 |
+
# ... [Keep your existing Arabic generator UI exactly as is] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Tab 2: Existing Quran Search
|
| 41 |
with gr.Tab("📖 القرآن الكريم"):
|
| 42 |
+
# ... [Keep your existing Quran search UI exactly as is] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Tab 3: New Tool Agent (CPU-optimized)
|
| 45 |
+
with gr.Tab("🛠️ مساعد ذكي"):
|
| 46 |
+
tool_input = gr.Textbox(label="اكتب طلبك هنا")
|
| 47 |
+
tool_output = gr.Textbox(label="النتيجة", lines=10)
|
| 48 |
+
run_btn = gr.Button("تشغيل")
|
| 49 |
+
|
| 50 |
+
def process_tool(input_text):
|
| 51 |
+
try:
|
| 52 |
+
tool_call = tool_agent.generate(input_text, TOOLS)
|
| 53 |
+
if tool_call["tool_name"] == "search_quran":
|
| 54 |
+
return quran.search(
|
| 55 |
+
tool_call["parameters"]["query"],
|
| 56 |
+
tool_call["parameters"]["max_results"]
|
| 57 |
+
)
|
| 58 |
+
elif tool_call["tool_name"] == "generate_text":
|
| 59 |
+
return text_gen.generate(
|
| 60 |
+
tool_call["parameters"]["prompt"],
|
| 61 |
+
tool_call["parameters"]["length"]
|
| 62 |
+
)
|
| 63 |
+
return "لم يتم التعرف على الأداة"
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return f"خطأ: {str(e)}"
|
| 66 |
|
| 67 |
+
run_btn.click(
|
| 68 |
+
lambda x: format_output(process_tool(x)),
|
| 69 |
inputs=tool_input,
|
| 70 |
outputs=tool_output
|
| 71 |
)
|