Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,149 +1,101 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Download
|
| 6 |
-
print("
|
| 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,
|
| 14 |
verbose=False,
|
| 15 |
)
|
| 16 |
-
|
| 17 |
print("✅ Model loaded!")
|
| 18 |
|
| 19 |
-
|
| 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.
|
| 24 |
-
2.
|
|
|
|
| 25 |
|
| 26 |
-
You
|
| 27 |
|
| 28 |
-
⚠️
|
| 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 |
-
|
| 56 |
-
partial_message = ""
|
| 57 |
for chunk in response:
|
| 58 |
-
if "choices"
|
| 59 |
-
|
| 60 |
-
|
| 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
|
| 71 |
-
["فشار خون بالا چه عوارضی دارد؟"],
|
| 72 |
-
["What are the warning signs of a heart attack?"],
|
| 73 |
]
|
| 74 |
|
| 75 |
-
|
| 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:
|
| 94 |
<h1>🩺 Pars-Medical-o1</h1>
|
| 95 |
-
<
|
| 96 |
-
<p
|
| 97 |
-
<a href="https://huggingface.co/HexQuant/Pars-Medical-o1-Llama-FFT"
|
| 98 |
-
<a href="https://twitter.com/xPOURY4"
|
| 99 |
-
<a href="https://github.com/xPOURY4" target="_blank">💻 GitHub</a>
|
| 100 |
</p>
|
| 101 |
</div>
|
| 102 |
""")
|
| 103 |
|
| 104 |
-
chatbot = gr.Chatbot(
|
| 105 |
-
|
| 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 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
-
gr.Examples(
|
| 122 |
-
examples=EXAMPLES,
|
| 123 |
-
inputs=msg,
|
| 124 |
-
label="💡 نمونه سوالات | Example Questions"
|
| 125 |
-
)
|
| 126 |
|
| 127 |
gr.HTML("""
|
| 128 |
-
<div style="text-align:
|
| 129 |
-
|
| 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 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
|
| 148 |
-
|
| 149 |
-
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Download model first
|
| 6 |
+
print("📥 Downloading GGUF model...")
|
| 7 |
+
model_path = hf_hub_download(
|
|
|
|
| 8 |
repo_id="HexQuant/Pars-Medical-o1-Llama-FFT-GGUF",
|
| 9 |
filename="Pars-Medical-o1-Llama-FFT-Q4_K_M.gguf",
|
| 10 |
+
)
|
| 11 |
+
print(f"✅ Model downloaded: {model_path}")
|
| 12 |
+
|
| 13 |
+
# Now import and load
|
| 14 |
+
from llama_cpp import Llama
|
| 15 |
+
|
| 16 |
+
print("🔄 Loading model...")
|
| 17 |
+
llm = Llama(
|
| 18 |
+
model_path=model_path,
|
| 19 |
n_ctx=2048,
|
| 20 |
n_threads=2,
|
| 21 |
+
n_gpu_layers=0,
|
| 22 |
verbose=False,
|
| 23 |
)
|
|
|
|
| 24 |
print("✅ Model loaded!")
|
| 25 |
|
| 26 |
+
SYSTEM_PROMPT = """You are Pars-Medical, a bilingual (Persian/English) medical AI assistant.
|
|
|
|
| 27 |
|
| 28 |
+
When answering medical questions:
|
| 29 |
+
1. Analyze the question carefully
|
| 30 |
+
2. Think step-by-step (you can use <think> tags for reasoning)
|
| 31 |
+
3. Provide accurate, helpful medical information
|
| 32 |
|
| 33 |
+
You respond in the same language as the user (Persian or English).
|
| 34 |
|
| 35 |
+
⚠️ Disclaimer: This is for educational purposes only. Always consult a real doctor."""
|
| 36 |
|
| 37 |
|
| 38 |
def respond(message, history):
|
|
|
|
|
|
|
|
|
|
| 39 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 40 |
|
|
|
|
| 41 |
for user_msg, assistant_msg in history:
|
| 42 |
messages.append({"role": "user", "content": user_msg})
|
| 43 |
if assistant_msg:
|
| 44 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 45 |
|
|
|
|
| 46 |
messages.append({"role": "user", "content": message})
|
| 47 |
|
|
|
|
| 48 |
response = llm.create_chat_completion(
|
| 49 |
messages=messages,
|
| 50 |
max_tokens=512,
|
| 51 |
temperature=0.7,
|
|
|
|
| 52 |
stream=True,
|
| 53 |
)
|
| 54 |
|
| 55 |
+
partial = ""
|
|
|
|
| 56 |
for chunk in response:
|
| 57 |
+
if chunk["choices"][0].get("delta", {}).get("content"):
|
| 58 |
+
partial += chunk["choices"][0]["delta"]["content"]
|
| 59 |
+
yield partial
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
|
|
|
| 62 |
EXAMPLES = [
|
| 63 |
["علائم دیابت نوع ۲ چیست؟"],
|
| 64 |
["What are the symptoms of hypothyroidism?"],
|
| 65 |
+
["سردرد میگرنی چه علائمی دارد؟"],
|
| 66 |
+
["How to differentiate viral from bacterial infection?"],
|
|
|
|
|
|
|
| 67 |
]
|
| 68 |
|
| 69 |
+
with gr.Blocks(title="🩺 Pars-Medical-o1") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
gr.HTML("""
|
| 71 |
+
<div style="text-align:center; padding:20px;">
|
| 72 |
<h1>🩺 Pars-Medical-o1</h1>
|
| 73 |
+
<p>دستیار پزشکی دوزبانه فارسی-انگلیسی | Bilingual Medical AI</p>
|
| 74 |
+
<p>
|
| 75 |
+
<a href="https://huggingface.co/HexQuant/Pars-Medical-o1-Llama-FFT">🤗 Model</a> |
|
| 76 |
+
<a href="https://twitter.com/xPOURY4">🐦 @xPOURY4</a>
|
|
|
|
| 77 |
</p>
|
| 78 |
</div>
|
| 79 |
""")
|
| 80 |
|
| 81 |
+
chatbot = gr.Chatbot(height=400)
|
| 82 |
+
msg = gr.Textbox(placeholder="سوال پزشکی خود را بپرسید... | Ask your question...", show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
+
submit = gr.Button("ارسال | Send", variant="primary")
|
| 86 |
+
clear = gr.Button("پاک کردن | Clear")
|
| 87 |
|
| 88 |
+
gr.Examples(examples=EXAMPLES, inputs=msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
gr.HTML("""
|
| 91 |
+
<div style="text-align:center; padding:10px; background:#fff3cd; border-radius:8px; margin-top:15px;">
|
| 92 |
+
⚠️ فقط برای آموزش | Educational purposes only - Not medical advice
|
|
|
|
|
|
|
|
|
|
| 93 |
</div>
|
| 94 |
+
<p style="text-align:center; color:#888; margin-top:10px;">Made with ❤️ by @xPOURY4 🇮🇷</p>
|
| 95 |
""")
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
msg.submit(respond, [msg, chatbot], chatbot).then(lambda: "", None, msg)
|
| 98 |
+
submit.click(respond, [msg, chatbot], chatbot).then(lambda: "", None, msg)
|
| 99 |
+
clear.click(lambda: None, None, chatbot)
|
|
|
|
| 100 |
|
| 101 |
+
demo.queue().launch()
|
|
|