Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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, file=None):
|
| 18 |
+
global history
|
| 19 |
+
|
| 20 |
+
content = message
|
| 21 |
+
|
| 22 |
+
# إضافة محتوى الملف إذا تم رفعه
|
| 23 |
+
if file:
|
| 24 |
+
file_content = file.read().decode()
|
| 25 |
+
content += f"\n\n# محتوى الملف:\n{file_content}"
|
| 26 |
+
|
| 27 |
+
# إضافة رسالة المستخدم للسجل
|
| 28 |
+
history.append({"role": "user", "content": content})
|
| 29 |
+
|
| 30 |
+
# طلب النموذج مع حفظ السياق
|
| 31 |
+
completion = client.chat.completions.create(
|
| 32 |
+
model="qwen/qwen3-coder-480b-a35b-instruct",
|
| 33 |
+
messages=history,
|
| 34 |
+
temperature=0,
|
| 35 |
+
top_p=0.61,
|
| 36 |
+
max_tokens=16384,
|
| 37 |
+
stream=True
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# جمع الرد من البث
|
| 41 |
+
response_text = ""
|
| 42 |
+
for chunk in completion:
|
| 43 |
+
if chunk.choices[0].delta.content is not None:
|
| 44 |
+
response_text += chunk.choices[0].delta.content
|
| 45 |
+
print(chunk.choices[0].delta.content, end="")
|
| 46 |
+
|
| 47 |
+
# إضافة الرد لسجل المحادثة
|
| 48 |
+
history.append({"role": "assistant", "content": response_text})
|
| 49 |
+
|
| 50 |
+
return response_text, history
|
| 51 |
+
|
| 52 |
+
# واجهة Gradio
|
| 53 |
+
with gr.Blocks() as demo:
|
| 54 |
+
gr.Markdown("# NVIDIA Qwen3 Chat")
|
| 55 |
+
chatbot = gr.Chatbot()
|
| 56 |
+
msg = gr.Textbox(label="اكتب رسالتك هنا")
|
| 57 |
+
file_input = gr.File(label="ملف Python أو نص", file_types=[".py", ".txt"])
|
| 58 |
+
send = gr.Button("إرسال")
|
| 59 |
+
|
| 60 |
+
def handle_submit(message, file):
|
| 61 |
+
answer, history = chat_with_model(message, file)
|
| 62 |
+
return gr.update(value=history), "", None
|
| 63 |
+
|
| 64 |
+
send.click(handle_submit, [msg, file_input], [chatbot, msg, file_input])
|
| 65 |
+
|
| 66 |
+
demo.launch()
|