File size: 2,272 Bytes
6ef8de9 45d0060 6ef8de9 45d0060 6ef8de9 45d0060 6ef8de9 596a293 6ef8de9 596a293 6ef8de9 596a293 6ef8de9 840b29c 6ef8de9 45d0060 6ef8de9 | 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | 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, a helpful assistant.
#Answer clearly and briefly.
#Reply in Arabic if the user writes in Arabic.
#Reply in English if the user writes in English.
#""".strip()
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() |