yyasso commited on
Commit
355a355
·
verified ·
1 Parent(s): a4ba35b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -18,19 +18,16 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
18
  IMAGE_WIDTH = 511
19
 
20
  def preprocess_image(image: Image.Image) -> Image.Image:
21
- # ضبط مقاس الصورة
22
  image_height = int(image.height * IMAGE_WIDTH / image.width)
23
  return image.resize((IMAGE_WIDTH, image_height))
24
 
25
  def save_image(image: Image.Image) -> str:
26
- # حفظ الصورة
27
  image_filename = f"{uuid.uuid4()}.jpeg"
28
  path = os.path.join(UPLOAD_FOLDER, image_filename)
29
  image.save(path, "JPEG", quality=95)
30
  return path
31
 
32
  def process_image(image_path: str) -> tuple:
33
- # تحويل وحفظ الصورة
34
  image = Image.open(image_path).convert('RGB')
35
  image = preprocess_image(image)
36
  saved_path = save_image(image)
@@ -45,20 +42,25 @@ def format_message(content, image_data=None):
45
  ]
46
  return content
47
 
48
- def upload_images(files, history):
 
 
49
  for f in files:
50
  saved, _ = process_image(f)
 
51
  history.append(("تم رفع صورة:", f"<img src='{saved}' style='max-width:200px;'>"))
52
- return history
53
 
54
- def chat_with_gpt(user_input, history, files):
55
- history = history or []
56
- messages = [{"role": "system", "content": "...relevant prompt..."}]
57
- if files:
58
- for f in files:
59
- saved, encoded = process_image(f)
60
- messages.append({"role":"user","content":format_message("وصف الصورة:", encoded)})
61
- messages.append({"role":"user","content":user_input})
 
 
62
 
63
  # استدعاء النموذج
64
  completion = client.chat.completions.create(
@@ -74,33 +76,39 @@ def chat_with_gpt(user_input, history, files):
74
  if chunk.choices[0].delta.content:
75
  response += chunk.choices[0].delta.content
76
  time.sleep(0.01)
77
- history.append((f"أنت: {user_input}", None))
78
- history.append((None, f"<img src='{BOT_AVATAR}' style='width:50px;border-radius:50%;'> {response}"))
79
- return history, history
80
 
 
 
 
 
 
81
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
82
  gr.Markdown("# 🤖 ShfraAI - مساعد ذكي لتحليل الصور")
83
- chatbot = gr.Chatbot(label="ShfraAI").style(height=600)
84
  user_input = gr.Textbox(placeholder="اكتب رسالتك...", show_label=False)
85
  upload_button = gr.UploadButton("رفع صور", file_count="multiple")
86
  submit_button = gr.Button("إرسال")
87
- state = gr.State()
 
 
 
 
 
 
 
88
 
89
  submit_button.click(
90
  chat_with_gpt,
91
- inputs=[user_input, state, upload_button],
92
- outputs=[chatbot, state]
93
  )
 
94
  user_input.submit(
95
  chat_with_gpt,
96
- inputs=[user_input, state, upload_button],
97
- outputs=[chatbot, state]
98
- )
99
- upload_button.upload(
100
- upload_images,
101
- inputs=[upload_button, state],
102
- outputs=[chatbot]
103
  )
104
 
105
  if __name__ == "__main__":
106
  demo.queue().launch(share=True)
 
 
18
  IMAGE_WIDTH = 511
19
 
20
  def preprocess_image(image: Image.Image) -> Image.Image:
 
21
  image_height = int(image.height * IMAGE_WIDTH / image.width)
22
  return image.resize((IMAGE_WIDTH, image_height))
23
 
24
  def save_image(image: Image.Image) -> str:
 
25
  image_filename = f"{uuid.uuid4()}.jpeg"
26
  path = os.path.join(UPLOAD_FOLDER, image_filename)
27
  image.save(path, "JPEG", quality=95)
28
  return path
29
 
30
  def process_image(image_path: str) -> tuple:
 
31
  image = Image.open(image_path).convert('RGB')
32
  image = preprocess_image(image)
33
  saved_path = save_image(image)
 
42
  ]
43
  return content
44
 
45
+ def upload_images(files, uploaded_images):
46
+ new_uploaded = uploaded_images.copy() if uploaded_images else []
47
+ history = []
48
  for f in files:
49
  saved, _ = process_image(f)
50
+ new_uploaded.append(saved)
51
  history.append(("تم رفع صورة:", f"<img src='{saved}' style='max-width:200px;'>"))
52
+ return history, new_uploaded
53
 
54
+ def chat_with_gpt(user_input, uploaded_images, chat_history):
55
+ chat_history = chat_history or []
56
+ messages = [{"role": "system", "content": "أنت مساعد ذكي تحلل الصور وتجاوب على استفسارات المستخدم بدقة وبأسلوب محترم."}]
57
+
58
+ if uploaded_images:
59
+ for image_path in uploaded_images:
60
+ _, encoded = process_image(image_path)
61
+ messages.append({"role": "user", "content": format_message("وصف الصورة:", encoded)})
62
+
63
+ messages.append({"role": "user", "content": user_input})
64
 
65
  # استدعاء النموذج
66
  completion = client.chat.completions.create(
 
76
  if chunk.choices[0].delta.content:
77
  response += chunk.choices[0].delta.content
78
  time.sleep(0.01)
 
 
 
79
 
80
+ chat_history.append({"role": "user", "content": user_input})
81
+ chat_history.append({"role": "assistant", "content": response})
82
+ return chat_history, uploaded_images
83
+
84
+ # واجهة Gradio
85
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
86
  gr.Markdown("# 🤖 ShfraAI - مساعد ذكي لتحليل الصور")
87
+ chatbot = gr.Chatbot(label="ShfraAI", avatar_images=(None, BOT_AVATAR), type="messages")
88
  user_input = gr.Textbox(placeholder="اكتب رسالتك...", show_label=False)
89
  upload_button = gr.UploadButton("رفع صور", file_count="multiple")
90
  submit_button = gr.Button("إرسال")
91
+ uploaded_state = gr.State([]) # لتخزين الصور
92
+ chat_history_state = gr.State([]) # لتخزين المحادثة
93
+
94
+ upload_button.upload(
95
+ upload_images,
96
+ inputs=[upload_button, uploaded_state],
97
+ outputs=[chatbot, uploaded_state]
98
+ )
99
 
100
  submit_button.click(
101
  chat_with_gpt,
102
+ inputs=[user_input, uploaded_state, chat_history_state],
103
+ outputs=[chatbot, chat_history_state]
104
  )
105
+
106
  user_input.submit(
107
  chat_with_gpt,
108
+ inputs=[user_input, uploaded_state, chat_history_state],
109
+ outputs=[chatbot, chat_history_state]
 
 
 
 
 
110
  )
111
 
112
  if __name__ == "__main__":
113
  demo.queue().launch(share=True)
114
+