Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,13 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
-
# 🔒 المفتاح السري من البيئة
|
| 6 |
API_KEY = os.getenv("API_KEY")
|
| 7 |
|
| 8 |
-
# إنشاء العميل
|
| 9 |
client = OpenAI(
|
| 10 |
base_url="https://integrate.api.nvidia.com/v1",
|
| 11 |
api_key=API_KEY
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# سجل المحادثة
|
| 15 |
history = []
|
| 16 |
|
| 17 |
def chat_with_model(message, files=None):
|
|
@@ -19,7 +16,7 @@ def chat_with_model(message, files=None):
|
|
| 19 |
|
| 20 |
content = message
|
| 21 |
|
| 22 |
-
#
|
| 23 |
if files:
|
| 24 |
for file in files:
|
| 25 |
try:
|
|
@@ -28,10 +25,8 @@ def chat_with_model(message, files=None):
|
|
| 28 |
file_content = "<تعذر قراءة الملف>"
|
| 29 |
content += f"\n\n# محتوى الملف {file.name}:\n{file_content}"
|
| 30 |
|
| 31 |
-
# إضافة رسالة المستخدم للسجل
|
| 32 |
history.append({"role": "user", "content": content})
|
| 33 |
|
| 34 |
-
# طلب النموذج مع حفظ السياق
|
| 35 |
completion = client.chat.completions.create(
|
| 36 |
model="qwen/qwen3-coder-480b-instruct",
|
| 37 |
messages=history,
|
|
@@ -41,14 +36,12 @@ def chat_with_model(message, files=None):
|
|
| 41 |
stream=True
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# جمع الرد من البث
|
| 45 |
response_text = ""
|
| 46 |
for chunk in completion:
|
| 47 |
if chunk.choices[0].delta.content is not None:
|
| 48 |
response_text += chunk.choices[0].delta.content
|
| 49 |
print(chunk.choices[0].delta.content, end="")
|
| 50 |
|
| 51 |
-
# إضافة الرد لسجل المحادثة
|
| 52 |
history.append({"role": "assistant", "content": response_text})
|
| 53 |
|
| 54 |
return response_text, history
|
|
@@ -56,9 +49,12 @@ def chat_with_model(message, files=None):
|
|
| 56 |
# واجهة Gradio
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
gr.Markdown("# NVIDIA Qwen3 Chat (متعدد الملفات)")
|
| 59 |
-
chatbot = gr.Chatbot()
|
|
|
|
| 60 |
msg = gr.Textbox(label="اكتب رسالتك هنا")
|
| 61 |
-
files_input = gr.File(label="ارفع ملفات Python أو نصية",
|
|
|
|
|
|
|
| 62 |
send = gr.Button("إرسال")
|
| 63 |
|
| 64 |
def handle_submit(message, files):
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
|
|
|
| 5 |
API_KEY = os.getenv("API_KEY")
|
| 6 |
|
|
|
|
| 7 |
client = OpenAI(
|
| 8 |
base_url="https://integrate.api.nvidia.com/v1",
|
| 9 |
api_key=API_KEY
|
| 10 |
)
|
| 11 |
|
|
|
|
| 12 |
history = []
|
| 13 |
|
| 14 |
def chat_with_model(message, files=None):
|
|
|
|
| 16 |
|
| 17 |
content = message
|
| 18 |
|
| 19 |
+
# قراءة محتوى الملفات المرفوعة
|
| 20 |
if files:
|
| 21 |
for file in files:
|
| 22 |
try:
|
|
|
|
| 25 |
file_content = "<تعذر قراءة الملف>"
|
| 26 |
content += f"\n\n# محتوى الملف {file.name}:\n{file_content}"
|
| 27 |
|
|
|
|
| 28 |
history.append({"role": "user", "content": content})
|
| 29 |
|
|
|
|
| 30 |
completion = client.chat.completions.create(
|
| 31 |
model="qwen/qwen3-coder-480b-instruct",
|
| 32 |
messages=history,
|
|
|
|
| 36 |
stream=True
|
| 37 |
)
|
| 38 |
|
|
|
|
| 39 |
response_text = ""
|
| 40 |
for chunk in completion:
|
| 41 |
if chunk.choices[0].delta.content is not None:
|
| 42 |
response_text += chunk.choices[0].delta.content
|
| 43 |
print(chunk.choices[0].delta.content, end="")
|
| 44 |
|
|
|
|
| 45 |
history.append({"role": "assistant", "content": response_text})
|
| 46 |
|
| 47 |
return response_text, history
|
|
|
|
| 49 |
# واجهة Gradio
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
gr.Markdown("# NVIDIA Qwen3 Chat (متعدد الملفات)")
|
| 52 |
+
chatbot = gr.Chatbot(type="messages") # تصحيح التحذير
|
| 53 |
+
|
| 54 |
msg = gr.Textbox(label="اكتب رسالتك هنا")
|
| 55 |
+
files_input = gr.File(label="ارفع ملفات Python أو نصية",
|
| 56 |
+
file_types=[".py", ".txt"],
|
| 57 |
+
file_types_multiple=True) # الاستخدام الصحيح للنسخ الحديثة
|
| 58 |
send = gr.Button("إرسال")
|
| 59 |
|
| 60 |
def handle_submit(message, files):
|