Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import io
|
|
| 5 |
from PIL import Image
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
# إعداد Logging
|
| 9 |
logging.basicConfig(
|
| 10 |
level=logging.INFO,
|
| 11 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
@@ -29,7 +28,6 @@ except Exception as e:
|
|
| 29 |
logger.error(f"❌ فشل استيراد الوحدات: {e}", exc_info=True)
|
| 30 |
raise
|
| 31 |
|
| 32 |
-
# Configuration
|
| 33 |
MODEL_NAME = "llama-3.1-8b-instant"
|
| 34 |
TEMPERATURE = 0.25
|
| 35 |
TOP_K = 3
|
|
@@ -77,13 +75,9 @@ def initialize_system():
|
|
| 77 |
logger.error(f"❌ فشل التهيئة: {e}", exc_info=True)
|
| 78 |
raise
|
| 79 |
|
| 80 |
-
# تهيئة النظام فور تحميل الملف
|
| 81 |
initialize_system()
|
| 82 |
|
| 83 |
-
# ==================== Gradio Functions ====================
|
| 84 |
-
|
| 85 |
def chat_with_agent(message: str, thread_id: str = "default") -> str:
|
| 86 |
-
"""دردشة مع الـ Agent"""
|
| 87 |
if agent is None: return "System not initialized yet."
|
| 88 |
|
| 89 |
session = get_session(thread_id)
|
|
@@ -110,7 +104,6 @@ def chat_with_agent(message: str, thread_id: str = "default") -> str:
|
|
| 110 |
return f"Error: {str(e)}"
|
| 111 |
|
| 112 |
def analyze_image(image, thread_id: str = "default") -> str:
|
| 113 |
-
"""تحليل صورة عبر الـ API الخارجية"""
|
| 114 |
if api_client is None: return "API client not initialized"
|
| 115 |
|
| 116 |
try:
|
|
@@ -134,21 +127,56 @@ def analyze_image(image, thread_id: str = "default") -> str:
|
|
| 134 |
logger.error(f"Image analysis error: {e}")
|
| 135 |
return f"Error: {str(e)}"
|
| 136 |
|
| 137 |
-
# ✅
|
| 138 |
-
api_info_md =
|
| 139 |
-
# 🏥 DermaScan AI - RAG API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
|
| 143 |
-
|
| 144 |
-
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
from gradio_client import Client
|
| 153 |
-
client = Client("Omarelrayes/bot3")
|
| 154 |
-
print(client.predict("ما هي أعراض سرطان الجلد؟", api_name="/chat_with_agent"))
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
| 8 |
logging.basicConfig(
|
| 9 |
level=logging.INFO,
|
| 10 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
|
|
| 28 |
logger.error(f"❌ فشل استيراد الوحدات: {e}", exc_info=True)
|
| 29 |
raise
|
| 30 |
|
|
|
|
| 31 |
MODEL_NAME = "llama-3.1-8b-instant"
|
| 32 |
TEMPERATURE = 0.25
|
| 33 |
TOP_K = 3
|
|
|
|
| 75 |
logger.error(f"❌ فشل التهيئة: {e}", exc_info=True)
|
| 76 |
raise
|
| 77 |
|
|
|
|
| 78 |
initialize_system()
|
| 79 |
|
|
|
|
|
|
|
| 80 |
def chat_with_agent(message: str, thread_id: str = "default") -> str:
|
|
|
|
| 81 |
if agent is None: return "System not initialized yet."
|
| 82 |
|
| 83 |
session = get_session(thread_id)
|
|
|
|
| 104 |
return f"Error: {str(e)}"
|
| 105 |
|
| 106 |
def analyze_image(image, thread_id: str = "default") -> str:
|
|
|
|
| 107 |
if api_client is None: return "API client not initialized"
|
| 108 |
|
| 109 |
try:
|
|
|
|
| 127 |
logger.error(f"Image analysis error: {e}")
|
| 128 |
return f"Error: {str(e)}"
|
| 129 |
|
| 130 |
+
# ✅ الحل: استخدام string concatenation بدل triple quotes
|
| 131 |
+
api_info_md = (
|
| 132 |
+
"# 🏥 DermaScan AI - RAG API\n\n"
|
| 133 |
+
"## الـ API شغالة! 🎉\n\n"
|
| 134 |
+
"Gradio يقوم تلقائياً بإنشاء API Endpoints للدوال:\n"
|
| 135 |
+
"- `analyze_image` → `/call/analyze_image`\n"
|
| 136 |
+
"- `chat_with_agent` → `/call/chat_with_agent`\n\n"
|
| 137 |
+
"### 📚 التوثيق الكامل:\n"
|
| 138 |
+
"افتح **Swagger UI** في `/docs` (إذا كان مفعلاً) أو استخدم Gradio Client.\n\n"
|
| 139 |
+
"### 🧪 اختبار الـ API عبر Python:\n\n"
|
| 140 |
+
"```python\n"
|
| 141 |
+
"from gradio_client import Client\n"
|
| 142 |
+
'client = Client("Omarelrayes/bot3")\n'
|
| 143 |
+
'print(client.predict("ما هي أعراض سرطان الجلد؟", api_name="/chat_with_agent"))\n'
|
| 144 |
+
"```\n"
|
| 145 |
+
)
|
| 146 |
|
| 147 |
+
logger.info("🎨 جاري بناء واجهة Gradio...")
|
| 148 |
|
| 149 |
+
with gr.Blocks(title="DermaScan AI") as demo:
|
| 150 |
+
gr.Markdown("# DermaScan AI - مساعد أمراض الجلدية")
|
| 151 |
+
|
| 152 |
+
with gr.Tab("📤 رفع الصورة"):
|
| 153 |
+
image_input = gr.Image(type="pil", label="ارفع صورة الجلد")
|
| 154 |
+
analyze_btn = gr.Button("🔍 تحليل الصورة", variant="primary")
|
| 155 |
+
analysis_output = gr.Markdown(label="نتيجة التحليل")
|
| 156 |
+
analyze_btn.click(fn=analyze_image, inputs=[image_input, gr.State("default")], outputs=[analysis_output])
|
| 157 |
+
|
| 158 |
+
with gr.Tab("💬 الدردشة"):
|
| 159 |
+
chatbot = gr.Chatbot(label="المحادثة", height=400)
|
| 160 |
+
msg_input = gr.Textbox(label="رسالتك", placeholder="اسأل عن حالتك الجلدية...", lines=2)
|
| 161 |
+
send_btn = gr.Button(" إرسال", variant="primary")
|
| 162 |
+
|
| 163 |
+
def user_message(user_msg, history):
|
| 164 |
+
return "", history + [[user_msg, None]]
|
| 165 |
+
|
| 166 |
+
def bot_response(history):
|
| 167 |
+
if not history: return history
|
| 168 |
+
history[-1][1] = chat_with_agent(history[-1][0], "default")
|
| 169 |
+
return history
|
| 170 |
+
|
| 171 |
+
send_btn.click(fn=user_message, inputs=[msg_input, chatbot], outputs=[msg_input, chatbot]).then(
|
| 172 |
+
fn=bot_response, inputs=[chatbot], outputs=[chatbot]
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
with gr.Tab("📚 معلومات الـ API"):
|
| 176 |
+
gr.Markdown(api_info_md)
|
| 177 |
|
| 178 |
+
logger.info("✅ تم بناء واجهة Gradio")
|
| 179 |
+
logger.info("🎉 DermaScan AI جاهز!")
|
| 180 |
|
| 181 |
+
if __name__ == "__main__":
|
| 182 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, debug=False)
|
|
|
|
|
|
|
|
|