FECUOY commited on
Commit
280cb4f
·
verified ·
1 Parent(s): ae5de09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -16
app.py CHANGED
@@ -1,18 +1,80 @@
1
- import os
2
  from huggingface_hub import InferenceClient
3
 
4
- client = InferenceClient(
5
- api_key=os.environ["HF_TOKEN"],
6
- )
7
-
8
- completion = client.chat.completions.create(
9
- model="moonshotai/Kimi-K2-Instruct-0905:groq",
10
- messages=[
11
- {
12
- "role": "user",
13
- "content": "What is the capital of France?"
14
- }
15
- ],
16
- )
17
-
18
- print(completion.choices[0].message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ def respond(
5
+ message, # هنا سيصبح message عبارة عن قاموس يحتوي على 'text' و 'files'
6
+ history: list[dict[str, str]],
7
+ system_message,
8
+ max_tokens,
9
+ temperature,
10
+ top_p,
11
+ hf_token: gr.OAuthToken,
12
+ ):
13
+ # التأكد من تسجيل الدخول
14
+ if hf_token is None:
15
+ yield "⚠️ يرجى تسجيل الدخول عبر Hugging Face أولاً."
16
+ return
17
+
18
+ client = InferenceClient(
19
+ model="moonshotai/Kimi-K2-Instruct",
20
+ token=hf_token.token
21
+ )
22
+
23
+ messages = [{"role": "system", "content": system_message}]
24
+ messages.extend(history)
25
+
26
+ # معالجة الرسالة المدخلة (نص + ملفات)
27
+ user_text = message.get("text", "")
28
+ files = message.get("files", [])
29
+
30
+ full_content = user_text
31
+ if files:
32
+ file_names = ", ".join([f.split('/')[-1] for f in files])
33
+ full_content += f"\n\n[المستخدم قام بإرفاق الملفات التالية: {file_names}]"
34
+ # ملاحظة: النماذج النصية لا "تقرأ" الملفات تلقائياً،
35
+ # ستحتاج لمكتبات مثل PyMuPDF للـ PDF لاستخراج النص فعلياً.
36
+
37
+ messages.append({"role": "user", "content": full_content})
38
+
39
+ response = ""
40
+ try:
41
+ for msg in client.chat_completion(
42
+ messages,
43
+ max_tokens=max_tokens,
44
+ stream=True,
45
+ temperature=temperature,
46
+ top_p=top_p,
47
+ ):
48
+ if msg.choices and len(msg.choices) > 0:
49
+ token = msg.choices[0].delta.content
50
+ if token:
51
+ response += token
52
+ yield response
53
+ except Exception as e:
54
+ yield f"⚠️ حدث خطأ: {str(e)}"
55
+
56
+ # إعداد واجهة Gradio مع دعم الوسائط المتعددة
57
+ chatbot_ui = gr.Chatbot(type="messages")
58
+
59
+ with gr.Blocks() as demo:
60
+ with gr.Sidebar():
61
+ gr.Markdown("### الإعدادات")
62
+ gr.LoginButton()
63
+ system_msg = gr.Textbox(value="أنت مساعد ذكي ومفيد.", label="System message")
64
+ max_t = gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max tokens")
65
+ temp = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
66
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
67
+
68
+ gr.ChatInterface(
69
+ respond,
70
+ chatbot=chatbot_ui,
71
+ type="messages",
72
+ multimodal=True, # تفعيل دعم الملفات
73
+ additional_inputs=[system_msg, max_t, temp, top_p],
74
+ title="Kimi-K2 Multimodal Chatbot",
75
+ # تحديد صيغ الملفات المسموح بها
76
+ file_types=[".pdf", ".docx", ".zip"]
77
+ )
78
+
79
+ if __name__ == "__main__":
80
+ demo.launch()