| import os |
| import gradio as gr |
| from openai import OpenAI |
|
|
| HF_TOKEN = os.environ.get("HF_TOKEN") |
| MODEL_ID = "Qwen/Qwen2.5-7B-Instruct" |
|
|
| if not HF_TOKEN: |
| raise ValueError("HF_TOKEN is missing. Add it in Space Settings -> Secrets.") |
|
|
| client = OpenAI( |
| base_url="https://router.huggingface.co/v1", |
| api_key=HF_TOKEN, |
| ) |
|
|
| |
| |
| |
| |
| |
| |
|
|
|
|
|
|
| SYSTEM_PROMPT = """ |
| You are Hadithi AI, an expert in hadith analysis. |
| |
| Your task is NOT just to answer, but to analyze hadith results in a professional and insightful way. |
| |
| Always structure your answer in this format: |
| |
| 1. π· Answer Insight: |
| - Clear, concise answer with deep understanding |
| |
| 2. π· What the Hadiths Cover: |
| - Bullet points summarizing themes |
| |
| 3. π· Key Finding: |
| - A smart insight inferred from the hadiths |
| |
| 4. π· Supporting Hadiths: |
| - Clean, readable formatting |
| - Keep Arabic text intact |
| - Mention source and number |
| |
| Rules: |
| - Be concise but insightful |
| - Avoid repetition |
| - Make the answer feel professional and "wow" |
| - Use a mix of clarity + intelligence |
| |
| Language: |
| - Arabic if user writes Arabic |
| - English otherwise |
| """.strip() |
|
|
|
|
|
|
| def chat(message, history): |
| messages = [{"role": "system", "content": SYSTEM_PROMPT}] |
|
|
| for user_msg, assistant_msg in history: |
| if user_msg: |
| messages.append({"role": "user", "content": user_msg}) |
| if assistant_msg: |
| messages.append({"role": "assistant", "content": assistant_msg}) |
|
|
| messages.append({"role": "user", "content": message}) |
|
|
| try: |
| response = client.chat.completions.create( |
| model=MODEL_ID, |
| messages=messages, |
| temperature=0.2, |
| max_tokens=900, |
| ) |
| answer = response.choices[0].message.content.strip() |
| except Exception as e: |
| answer = f"Error: {str(e)}" |
|
|
| return answer |
|
|
| demo = gr.ChatInterface( |
| fn=chat, |
| title="Hadithi AI", |
| description="Chat with Qwen2.5-7B-Instruct", |
| textbox=gr.Textbox( |
| placeholder="Ask anything here...", |
| container=True, |
| scale=7, |
| ), |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |