yyasso commited on
Commit
ec8348d
·
verified ·
1 Parent(s): 3e8441f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ import os
4
+ from PIL import Image
5
+ import base64
6
+ import uuid
7
+ import time
8
+
9
+ API_KEY = "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"
10
+ client = Groq(api_key=API_KEY)
11
+
12
+ # إعداد المجلدات والصور
13
+ UPLOAD_FOLDER = "./uploaded_images"
14
+ BOT_AVATAR = "https://shfra.netlify.app/9121959e-df49-4796-a8b2-eb85f6368976.png"
15
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
16
+
17
+ # إعدادات الصور
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)
37
+ encoded = base64.b64encode(open(saved_path, 'rb').read()).decode('utf-8')
38
+ return saved_path, encoded
39
+
40
+ def format_message(content, image_data=None):
41
+ if image_data:
42
+ return [
43
+ {"type": "text", "text": content},
44
+ {"type": "image_url", "image_url": f"data:image/jpeg;base64,{image_data}"}
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(
65
+ model="llama-3.3-70b-versatile",
66
+ messages=messages,
67
+ temperature=0.7,
68
+ max_tokens=1024,
69
+ top_p=1,
70
+ stream=True
71
+ )
72
+ response = ""
73
+ for chunk in completion:
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)