Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,91 @@
|
|
| 1 |
-
import
|
| 2 |
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def respond(message, history, system_message, max_tokens, temperature):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if client is None:
|
| 19 |
-
yield "⚠️ خطأ: لم يتم العثور على MOONSHOT_API_KEY في إعدادات Secrets."
|
| 20 |
return
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
+
import zipfile
|
| 4 |
+
import os
|
| 5 |
+
import fitz # PyMuPDF
|
| 6 |
+
import docx
|
| 7 |
|
| 8 |
+
# جلب المفتاح من إعدادات Secrets
|
| 9 |
+
API_KEY = os.environ.get("MOONSHOT_API_KEY")
|
| 10 |
|
| 11 |
+
def extract_text_from_file(file_path):
|
| 12 |
+
try:
|
| 13 |
+
filename = os.path.basename(file_path)
|
| 14 |
+
ext = filename.split('.')[-1].lower()
|
| 15 |
+
if ext == 'pdf':
|
| 16 |
+
with fitz.open(file_path) as doc:
|
| 17 |
+
return f"\n[PDF: {filename}]\n" + "".join([p.get_text() for p in doc])[:30000]
|
| 18 |
+
elif ext == 'docx':
|
| 19 |
+
doc_obj = docx.Document(file_path)
|
| 20 |
+
return f"\n[Word: {filename}]\n" + "\n".join([p.text for p in doc_obj.paragraphs])[:30000]
|
| 21 |
+
elif ext == 'zip':
|
| 22 |
+
report = f"\n[ZIP: {filename}]\n"
|
| 23 |
+
with zipfile.ZipFile(file_path, 'r') as z:
|
| 24 |
+
files = z.namelist()
|
| 25 |
+
report += f"يحتوي المجلد على {len(files)} ملف. فهرس أول 50 ملف:\n" + "\n".join(files[:50])
|
| 26 |
+
return report
|
| 27 |
+
else:
|
| 28 |
+
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 29 |
+
return f"\n[File: {filename}]\n" + f.read()[:20000]
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"\n⚠️ خطأ في قراءة {os.path.basename(file_path)}: {str(e)}\n"
|
| 32 |
|
| 33 |
def respond(message, history, system_message, max_tokens, temperature):
|
| 34 |
+
if not API_KEY:
|
| 35 |
+
yield "⚠️ خطأ: يرجى إضافة MOONSHOT_API_KEY في Secrets."
|
|
|
|
|
|
|
| 36 |
return
|
| 37 |
|
| 38 |
+
client = OpenAI(api_key=API_KEY, base_url="https://api.moonshot.cn/v1")
|
| 39 |
+
|
| 40 |
+
user_text = message.get("text", "")
|
| 41 |
+
files = message.get("files", [])
|
| 42 |
+
|
| 43 |
+
context = ""
|
| 44 |
+
if files:
|
| 45 |
+
yield "⏳ جاري تحليل ملفاتك بواسطة Kimi-K2..."
|
| 46 |
+
for f in files:
|
| 47 |
+
path = f if isinstance(f, str) else f.name
|
| 48 |
+
context += extract_text_from_file(path)
|
| 49 |
+
|
| 50 |
+
messages = [{"role": "system", "content": system_message}]
|
| 51 |
+
for msg in history:
|
| 52 |
+
messages.append(msg)
|
| 53 |
+
|
| 54 |
+
full_user_msg = f"{context}\n\nالسؤال: {user_text}"
|
| 55 |
+
messages.append({"role": "user", "content": full_user_msg})
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
completion = client.chat.completions.create(
|
| 59 |
+
model="moonshot-v1-128k",
|
| 60 |
+
messages=messages,
|
| 61 |
+
temperature=temperature,
|
| 62 |
+
max_tokens=max_tokens,
|
| 63 |
+
stream=True,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
response = ""
|
| 67 |
+
for chunk in completion:
|
| 68 |
+
content = chunk.choices[0].delta.content
|
| 69 |
+
if content:
|
| 70 |
+
response += content
|
| 71 |
+
yield response
|
| 72 |
+
except Exception as e:
|
| 73 |
+
yield f"⚠️ خطأ من سيرفر Moonshot: {str(e)}"
|
| 74 |
+
|
| 75 |
+
# واجهة المستخدم
|
| 76 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 77 |
+
gr.Markdown("# 🤖 Kimi-K2 (Moonshot AI) File Analyzer")
|
| 78 |
+
gr.ChatInterface(
|
| 79 |
+
respond,
|
| 80 |
+
multimodal=True,
|
| 81 |
+
type="messages",
|
| 82 |
+
additional_inputs=[
|
| 83 |
+
gr.Textbox(value="أنت مساعد ذكي يعتمد على نموذج Kimi-K2.", label="نظام المساعد"),
|
| 84 |
+
gr.Slider(512, 16384, value=4096, label="Max Tokens"),
|
| 85 |
+
gr.Slider(0.1, 1.5, value=0.7, label="Temperature"),
|
| 86 |
+
]
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# التعديل الجوهري للتشغيل في Hugging Face Spaces
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|