Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from groq import Groq | |
| import os | |
| from PIL import Image | |
| API_KEY = "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" | |
| client = Groq(api_key=API_KEY) | |
| # مسار لحفظ الصور المرفوعة | |
| UPLOAD_FOLDER = "./uploaded_images" | |
| os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
| # صورة رمزية للبوت | |
| BOT_AVATAR = "https://storage.googleapis.com/aiagents_1/agent-logos/1735859484526-oC9VurEz400x400.jpg" | |
| def save_and_process_image(image_path): | |
| """حفظ الصورة المرفوعة ومعالجتها""" | |
| image = Image.open(image_path) | |
| saved_image_path = os.path.join(UPLOAD_FOLDER, os.path.basename(image_path)) | |
| image.save(saved_image_path) | |
| return saved_image_path | |
| def chat_with_gpt(user_input, history, image): | |
| history = history or [] | |
| messages = [ | |
| {"role": "system", "content": "Your name is ASI. Your name is an abbreviation of a sentence ai super intelligence. The company that develops you is called hcoding. You are made of a model groq."}, | |
| {"role": "user", "content": user_input} | |
| ] | |
| # معالجة الصورة إذا تم رفعها | |
| if image: | |
| saved_image_path = save_and_process_image(image) | |
| messages.append({"role": "user", "content": f""}) | |
| image_html = f"<img src='{saved_image_path}' style='max-width: 200px; max-height: 200px;'>" | |
| history.append(("User uploaded an image:", image_html)) | |
| # استدعاء نموذج الذكاء الاصطناعي | |
| completion = client.chat.completions.create( | |
| model="llama-3.3-70b-versatile", | |
| messages=messages, | |
| temperature=1, | |
| max_tokens=1024, | |
| top_p=1, | |
| stream=True, | |
| stop=None, | |
| ) | |
| # تجميع الرد من البوت | |
| response = "" | |
| for chunk in completion: | |
| if chunk.choices[0].delta.content: | |
| response += chunk.choices[0].delta.content | |
| # تحديث السجل مع عرض صورة رمزية للبوت | |
| history.append((f"You: {user_input}", None)) # رسالة المستخدم | |
| history.append((None, f"<img src='{BOT_AVATAR}' style='width: 50px; border-radius: 50%;'> {response}")) # رسالة البوت | |
| return history, history, None # إعادة تعيين الحقل الخاص بالصورة إلى None | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| chatbot = gr.Chatbot(label="RefridGPT Chat", elem_id="chatbox") | |
| with gr.Row(): | |
| user_input = gr.Textbox(label="Input", placeholder="Type your message here...", scale=5) | |
| submit_button = gr.Button("Send") | |
| with gr.Row(): | |
| image_input = gr.Image(label="Choose Image", type="filepath") | |
| state = gr.State() # لتخزين التاريخ | |
| # حدث زر الإرسال | |
| submit_button.click(chat_with_gpt, inputs=[user_input, state, image_input], outputs=[chatbot, state, image_input]) | |
| demo.launch() | |