File size: 1,905 Bytes
c3e125f 6c2a904 c3e125f 6c2a904 fe7d607 6c2a904 c3e125f 6c2a904 fe7d607 6c2a904 fe7d607 c3e125f 6c2a904 c3e125f 6c2a904 c3e125f 6c2a904 f094a3c c3e125f 6c2a904 c3e125f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import os
import gradio as gr
from openai import OpenAI
# احصلي API Key وضعيه كـ متغير بيئي
API_KEY = os.environ.get("DEEPSEEK_API_KEY")
if not API_KEY:
raise ValueError("ضع DEEPSEEK_API_KEY في البيئة")
# تهيئة العميل لاستخدام DeepSeek عبر openai SDK
client = OpenAI(api_key=API_KEY, base_url="https://api.deepseek.com")
MODEL_NAME = "deepseek-chat" # أو deepseek-reasoner حسب الموديل اللي تستخدمينه
def generate_main_question_deepseek(paragraph: str):
if not paragraph.strip():
return "الرجاء إدخال فقرة أولاً."
# نجهز الرسائل بالطريقة المعتمدة في API شات
messages = [
{"role": "system", "content": "أنت مساعد ذكي."},
{"role": "user", "content": f"الفقرة التالية:\n{paragraph}\n\nالمطلوب: توليد سؤال أساسي فقط حسب الفقرة."}
]
try:
response = client.chat.completions.create(
model=MODEL_NAME,
messages=messages,
temperature=0.7,
max_tokens=100
)
# نأخذ الجواب من الرسالة الأولى
question = response.choices[0].message.content.strip()
return question
except Exception as e:
return f"Error while connecting to DeepSeek API: {e}"
with gr.Blocks() as demo:
gr.Markdown("## مولد سؤال أساسي باستخدام DeepSeek")
paragraph = gr.Textbox(label="Paragraph (Input text)", lines=8, placeholder="أدخل الفقرة هنا …")
output = gr.Textbox(label="السؤال المولد (بالعربية)", lines=3)
submit_btn = gr.Button("توليد")
submit_btn.click(fn=generate_main_question_deepseek, inputs=paragraph, outputs=output)
if __name__ == "__main__":
demo.launch(share=True, show_error=True)
|