| | import os |
| | import gradio as gr |
| | from openai import OpenAI |
| |
|
| | |
| | API_KEY = os.environ.get("DEEPSEEK_API_KEY") |
| | if not API_KEY: |
| | raise ValueError("ضع DEEPSEEK_API_KEY في البيئة") |
| |
|
| | |
| | client = OpenAI(api_key=API_KEY, base_url="https://api.deepseek.com") |
| |
|
| | MODEL_NAME = "deepseek-chat" |
| |
|
| | def generate_main_question_deepseek(paragraph: str): |
| | if not paragraph.strip(): |
| | return "الرجاء إدخال فقرة أولاً." |
| |
|
| | |
| | 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) |
| |
|