Spaces:
Build error
Build error
Upload 3 files
Browse files- SPACE_README.md +37 -0
- app.py +149 -0
- requirements.txt +3 -0
SPACE_README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Pars-Medical-o1 Chatbot
|
| 3 |
+
emoji: 🩺
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: teal
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
models:
|
| 12 |
+
- HexQuant/Pars-Medical-o1-Llama-FFT-GGUF
|
| 13 |
+
short_description: Bilingual Persian-English Medical AI Assistant
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# 🩺 Pars-Medical-o1 Chatbot
|
| 17 |
+
|
| 18 |
+
**دستیار هوش مصنوعی پزشکی دوزبانه | Bilingual Medical AI Assistant**
|
| 19 |
+
|
| 20 |
+
This Space runs [Pars-Medical-o1-Llama-FFT](https://huggingface.co/HexQuant/Pars-Medical-o1-Llama-FFT) - the first bilingual (Persian/English) medical AI with chain-of-thought reasoning.
|
| 21 |
+
|
| 22 |
+
## Features
|
| 23 |
+
- 🌐 Bilingual: Persian (Farsi) & English
|
| 24 |
+
- 🧠 Chain-of-Thought reasoning
|
| 25 |
+
- 🏥 Medical knowledge base
|
| 26 |
+
- ⚡ Fast inference with GGUF
|
| 27 |
+
|
| 28 |
+
## Links
|
| 29 |
+
- 🤗 [Main Model](https://huggingface.co/HexQuant/Pars-Medical-o1-Llama-FFT)
|
| 30 |
+
- 📦 [GGUF Version](https://huggingface.co/HexQuant/Pars-Medical-o1-Llama-FFT-GGUF)
|
| 31 |
+
- 🐦 [Twitter @xPOURY4](https://twitter.com/xPOURY4)
|
| 32 |
+
|
| 33 |
+
## ⚠️ Disclaimer
|
| 34 |
+
This is for **educational purposes only**. Not a substitute for professional medical advice.
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
Made with ❤️ by [@xPOURY4](https://twitter.com/xPOURY4) in Iran 🇮🇷
|
app.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Download and load model
|
| 6 |
+
print("🔄 Loading model...")
|
| 7 |
+
|
| 8 |
+
llm = Llama.from_pretrained(
|
| 9 |
+
repo_id="HexQuant/Pars-Medical-o1-Llama-FFT-GGUF",
|
| 10 |
+
filename="Pars-Medical-o1-Llama-FFT-Q4_K_M.gguf",
|
| 11 |
+
n_ctx=2048,
|
| 12 |
+
n_threads=2,
|
| 13 |
+
n_gpu_layers=0, # CPU only for free tier
|
| 14 |
+
verbose=False,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
print("✅ Model loaded!")
|
| 18 |
+
|
| 19 |
+
# System prompt
|
| 20 |
+
SYSTEM_PROMPT = """You are Pars-Medical, a bilingual (Persian/English) medical AI assistant with chain-of-thought reasoning.
|
| 21 |
+
|
| 22 |
+
When answering:
|
| 23 |
+
1. First, analyze the question in <think> tags
|
| 24 |
+
2. Then provide a clear, helpful answer
|
| 25 |
+
|
| 26 |
+
You can respond in both Persian (Farsi) and English based on the user's language.
|
| 27 |
+
|
| 28 |
+
⚠️ Important: You are for educational purposes only. Always recommend consulting a real doctor for medical decisions."""
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def respond(message, history):
|
| 32 |
+
"""Generate response for the chatbot"""
|
| 33 |
+
|
| 34 |
+
# Build messages
|
| 35 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 36 |
+
|
| 37 |
+
# Add history
|
| 38 |
+
for user_msg, assistant_msg in history:
|
| 39 |
+
messages.append({"role": "user", "content": user_msg})
|
| 40 |
+
if assistant_msg:
|
| 41 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 42 |
+
|
| 43 |
+
# Add current message
|
| 44 |
+
messages.append({"role": "user", "content": message})
|
| 45 |
+
|
| 46 |
+
# Generate response
|
| 47 |
+
response = llm.create_chat_completion(
|
| 48 |
+
messages=messages,
|
| 49 |
+
max_tokens=512,
|
| 50 |
+
temperature=0.7,
|
| 51 |
+
top_p=0.9,
|
| 52 |
+
stream=True,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Stream response
|
| 56 |
+
partial_message = ""
|
| 57 |
+
for chunk in response:
|
| 58 |
+
if "choices" in chunk and len(chunk["choices"]) > 0:
|
| 59 |
+
delta = chunk["choices"][0].get("delta", {})
|
| 60 |
+
if "content" in delta:
|
| 61 |
+
partial_message += delta["content"]
|
| 62 |
+
yield partial_message
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Example questions
|
| 66 |
+
EXAMPLES = [
|
| 67 |
+
["علائم دیابت نوع ۲ چیست؟"],
|
| 68 |
+
["What are the symptoms of hypothyroidism?"],
|
| 69 |
+
["سردرد میگرنی چه تفاوتی با سردرد تنشی دارد؟"],
|
| 70 |
+
["How do I differentiate between viral and bacterial infections?"],
|
| 71 |
+
["فشار خون بالا چه عوارضی دارد؟"],
|
| 72 |
+
["What are the warning signs of a heart attack?"],
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
# Custom CSS
|
| 76 |
+
CSS = """
|
| 77 |
+
.gradio-container {
|
| 78 |
+
font-family: 'Vazirmatn', 'Segoe UI', sans-serif !important;
|
| 79 |
+
}
|
| 80 |
+
.contain {
|
| 81 |
+
max-width: 900px !important;
|
| 82 |
+
margin: auto !important;
|
| 83 |
+
}
|
| 84 |
+
footer {
|
| 85 |
+
display: none !important;
|
| 86 |
+
}
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
# Build Gradio interface
|
| 90 |
+
with gr.Blocks(css=CSS, title="🩺 Pars-Medical AI") as demo:
|
| 91 |
+
|
| 92 |
+
gr.HTML("""
|
| 93 |
+
<div style="text-align: center; padding: 20px;">
|
| 94 |
+
<h1>🩺 Pars-Medical-o1</h1>
|
| 95 |
+
<h3>دستیار هوش مصنوعی پزشکی دوزبانه | Bilingual Medical AI Assistant</h3>
|
| 96 |
+
<p style="color: #666;">
|
| 97 |
+
<a href="https://huggingface.co/HexQuant/Pars-Medical-o1-Llama-FFT" target="_blank">🤗 Model</a> |
|
| 98 |
+
<a href="https://twitter.com/xPOURY4" target="_blank">🐦 Twitter</a> |
|
| 99 |
+
<a href="https://github.com/xPOURY4" target="_blank">💻 GitHub</a>
|
| 100 |
+
</p>
|
| 101 |
+
</div>
|
| 102 |
+
""")
|
| 103 |
+
|
| 104 |
+
chatbot = gr.Chatbot(
|
| 105 |
+
height=450,
|
| 106 |
+
placeholder="سوال پزشکی خود را بپرسید... | Ask your medical question...",
|
| 107 |
+
show_label=False,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
msg = gr.Textbox(
|
| 111 |
+
placeholder="پیام خود را بنویسید... | Type your message...",
|
| 112 |
+
show_label=False,
|
| 113 |
+
container=False,
|
| 114 |
+
scale=7,
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Row():
|
| 118 |
+
submit_btn = gr.Button("ارسال | Send", variant="primary", scale=1)
|
| 119 |
+
clear_btn = gr.Button("پاک کردن | Clear", scale=1)
|
| 120 |
+
|
| 121 |
+
gr.Examples(
|
| 122 |
+
examples=EXAMPLES,
|
| 123 |
+
inputs=msg,
|
| 124 |
+
label="💡 نمونه سوالات | Example Questions"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
gr.HTML("""
|
| 128 |
+
<div style="text-align: center; padding: 15px; margin-top: 20px; background: #fff3cd; border-radius: 10px;">
|
| 129 |
+
<p style="color: #856404; margin: 0;">
|
| 130 |
+
⚠️ <b>توجه:</b> این مدل صرفاً جهت آموزش است و جایگزین مشاوره پزشکی نیست.<br>
|
| 131 |
+
⚠️ <b>Disclaimer:</b> This is for educational purposes only. Not a substitute for professional medical advice.
|
| 132 |
+
</p>
|
| 133 |
+
</div>
|
| 134 |
+
""")
|
| 135 |
+
|
| 136 |
+
gr.HTML("""
|
| 137 |
+
<div style="text-align: center; padding: 10px; color: #888;">
|
| 138 |
+
Made with ❤️ by <a href="https://twitter.com/xPOURY4">@xPOURY4</a> in Iran 🇮🇷
|
| 139 |
+
</div>
|
| 140 |
+
""")
|
| 141 |
+
|
| 142 |
+
# Event handlers
|
| 143 |
+
msg.submit(respond, [msg, chatbot], chatbot).then(lambda: "", None, msg)
|
| 144 |
+
submit_btn.click(respond, [msg, chatbot], chatbot).then(lambda: "", None, msg)
|
| 145 |
+
clear_btn.click(lambda: None, None, chatbot)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
if __name__ == "__main__":
|
| 149 |
+
demo.queue().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
llama-cpp-python>=0.2.0
|
| 3 |
+
huggingface_hub>=0.20.0
|