Omarelrayes commited on
Commit
e07547d
·
verified ·
1 Parent(s): 7a1870f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -53
app.py CHANGED
@@ -39,12 +39,8 @@ api_client = None
39
  def get_session(thread_id: str) -> dict:
40
  if thread_id not in SESSIONS:
41
  SESSIONS[thread_id] = {
42
- "role": "patient",
43
- "last_analysis": None,
44
- "current_image": None,
45
- "_image_blobs": {},
46
- "_session_image_names": [],
47
- "_turn_count": 0
48
  }
49
  return SESSIONS[thread_id]
50
 
@@ -89,10 +85,7 @@ def chat_with_agent(message: str, history: list) -> str:
89
 
90
  try:
91
  last_analysis = session.get("last_analysis")
92
- analysis_ctx = ""
93
- if last_analysis:
94
- analysis_ctx = f"\n\n[Analysis: {last_analysis.get('label', 'N/A')}]"
95
-
96
  full_prompt = message + analysis_ctx
97
  config = {"configurable": {"thread_id": "default"}, "recursion_limit": 14}
98
 
@@ -144,11 +137,10 @@ def analyze_image(image, thread_id: str = "default") -> str:
144
 
145
  logger.info("جاري بناء واجهة Gradio...")
146
 
147
- # ✅ بناء الواجهة باستخدام Chatbot يدوي (مضمون 100%)
148
  with gr.Blocks(title="DermaScan AI") as demo:
149
  gr.Markdown("# 🏥 DermaScan AI - مساعد أمراض الجلدية")
150
 
151
- with gr.Tab("📤 رفع الصورة"):
152
  image_input = gr.Image(type="pil", label="ارفع صورة الجلد")
153
  analyze_btn = gr.Button("🔍 تحليل الصورة", variant="primary")
154
  analysis_output = gr.Textbox(label="نتيجة التحليل", lines=6)
@@ -156,13 +148,9 @@ with gr.Blocks(title="DermaScan AI") as demo:
156
 
157
  with gr.Tab("💬 الدردشة"):
158
  chatbot = gr.Chatbot(label="المحادثة", height=400)
159
- msg_input = gr.Textbox(
160
- label="رسالتك",
161
- placeholder="اسأل عن حالتك الجلدية...",
162
- lines=2
163
- )
164
  send_btn = gr.Button("🚀 إرسال", variant="primary")
165
- clear_btn = gr.Button("🗑️ مسح المحادثة")
166
 
167
  def user_message(user_msg, history):
168
  return "", history + [[user_msg, None]]
@@ -175,45 +163,26 @@ with gr.Blocks(title="DermaScan AI") as demo:
175
  history[-1][1] = response
176
  return history
177
 
178
- def clear_chat():
179
- return None
180
-
181
- send_btn.click(
182
- fn=user_message,
183
- inputs=[msg_input, chatbot],
184
- outputs=[msg_input, chatbot]
185
- ).then(
186
- fn=bot_response,
187
- inputs=[chatbot],
188
- outputs=[chatbot]
189
  )
190
-
191
- msg_input.submit(
192
- fn=user_message,
193
- inputs=[msg_input, chatbot],
194
- outputs=[msg_input, chatbot]
195
- ).then(
196
- fn=bot_response,
197
- inputs=[chatbot],
198
- outputs=[chatbot]
199
  )
200
-
201
- clear_btn.click(fn=clear_chat, outputs=[chatbot])
202
 
203
- with gr.Tab("📚 معلومات الـ API"):
204
- gr.Markdown(
205
- "# 🏥 DermaScan AI - RAG API\n\n"
206
- "## الـ API شغالة! 🎉\n\n"
207
- "Gradio يقوم تلقائياً بإنشاء API Endpoints للدوال.\n\n"
208
- "### 🧪 اختبار الـ API عبر Python:\n\n"
209
- "```python\n"
210
- "from gradio_client import Client\n"
211
- 'client = Client("Omarelrayes/bot3")\n'
212
- 'print(client.predict("ما هي أعراض سرطان الجلد؟", fn_index=0))\n'
213
- "```\n"
214
- )
215
 
216
  logger.info("✅ تم بناء واجهة Gradio")
217
  logger.info("🎉 DermaScan AI جاهز!")
218
 
219
- # ✅ لا تستدعي demo.launch() - HF Spaces يشغل تلقائياً
 
 
 
 
 
 
 
 
 
39
  def get_session(thread_id: str) -> dict:
40
  if thread_id not in SESSIONS:
41
  SESSIONS[thread_id] = {
42
+ "role": "patient", "last_analysis": None, "current_image": None,
43
+ "_image_blobs": {}, "_session_image_names": [], "_turn_count": 0
 
 
 
 
44
  }
45
  return SESSIONS[thread_id]
46
 
 
85
 
86
  try:
87
  last_analysis = session.get("last_analysis")
88
+ analysis_ctx = f"\n\n[Analysis: {last_analysis.get('label', 'N/A')}]" if last_analysis else ""
 
 
 
89
  full_prompt = message + analysis_ctx
90
  config = {"configurable": {"thread_id": "default"}, "recursion_limit": 14}
91
 
 
137
 
138
  logger.info("جاري بناء واجهة Gradio...")
139
 
 
140
  with gr.Blocks(title="DermaScan AI") as demo:
141
  gr.Markdown("# 🏥 DermaScan AI - مساعد أمراض الجلدية")
142
 
143
+ with gr.Tab(" رفع الصورة"):
144
  image_input = gr.Image(type="pil", label="ارفع صورة الجلد")
145
  analyze_btn = gr.Button("🔍 تحليل الصورة", variant="primary")
146
  analysis_output = gr.Textbox(label="نتيجة التحليل", lines=6)
 
148
 
149
  with gr.Tab("💬 الدردشة"):
150
  chatbot = gr.Chatbot(label="المحادثة", height=400)
151
+ msg_input = gr.Textbox(label="رسالتك", placeholder="اسأل عن حالتك الجلدية...", lines=2)
 
 
 
 
152
  send_btn = gr.Button("🚀 إرسال", variant="primary")
153
+ clear_btn = gr.Button("🗑️ مسح")
154
 
155
  def user_message(user_msg, history):
156
  return "", history + [[user_msg, None]]
 
163
  history[-1][1] = response
164
  return history
165
 
166
+ send_btn.click(user_message, [msg_input, chatbot], [msg_input, chatbot]).then(
167
+ bot_response, chatbot, chatbot
 
 
 
 
 
 
 
 
 
168
  )
169
+ msg_input.submit(user_message, [msg_input, chatbot], [msg_input, chatbot]).then(
170
+ bot_response, chatbot, chatbot
 
 
 
 
 
 
 
171
  )
172
+ clear_btn.click(lambda: None, None, chatbot)
 
173
 
174
+ with gr.Tab(" API Info"):
175
+ gr.Markdown("## API شغالة! استخدم Gradio Client للاتصال.")
 
 
 
 
 
 
 
 
 
 
176
 
177
  logger.info("✅ تم بناء واجهة Gradio")
178
  logger.info("🎉 DermaScan AI جاهز!")
179
 
180
+ # ✅ تشغيل Gradio - ده اللي هيخلي التطبيق يفضل شغال
181
+ demo.launch(
182
+ server_name="0.0.0.0",
183
+ server_port=7860,
184
+ debug=False,
185
+ share=False,
186
+ show_error=True,
187
+ prevent_thread_lock=True
188
+ )