Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
# Download model
|
| 6 |
print("📥 Downloading GGUF model...")
|
| 7 |
model_path = hf_hub_download(
|
| 8 |
repo_id="HexQuant/Pars-Medical-o1-Llama-FFT-GGUF",
|
| 9 |
-
filename="
|
| 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...")
|
|
@@ -21,81 +19,63 @@ llm = Llama(
|
|
| 21 |
n_gpu_layers=0,
|
| 22 |
verbose=False,
|
| 23 |
)
|
| 24 |
-
print("✅ Model
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 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 |
-
|
| 56 |
for chunk in response:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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 |
-
<
|
| 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=
|
| 82 |
-
msg = gr.Textbox(placeholder="
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
-
|
| 86 |
-
clear = gr.Button("پاک کردن | Clear")
|
| 87 |
|
| 88 |
-
gr.Examples(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
gr.HTML("""
|
| 91 |
-
<div style="
|
| 92 |
-
⚠️ فقط برای آموزش
|
|
|
|
| 93 |
</div>
|
| 94 |
-
<p style="text-align:center; color:#
|
| 95 |
""")
|
| 96 |
|
| 97 |
-
msg.submit(
|
| 98 |
-
|
| 99 |
clear.click(lambda: None, None, chatbot)
|
| 100 |
|
| 101 |
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 3 |
|
| 4 |
+
# Download model
|
| 5 |
print("📥 Downloading GGUF model...")
|
| 6 |
model_path = hf_hub_download(
|
| 7 |
repo_id="HexQuant/Pars-Medical-o1-Llama-FFT-GGUF",
|
| 8 |
+
filename="model-Q4_K_M.gguf", # ✅ Fixed filename
|
| 9 |
)
|
| 10 |
print(f"✅ Model downloaded: {model_path}")
|
| 11 |
|
|
|
|
| 12 |
from llama_cpp import Llama
|
| 13 |
|
| 14 |
print("🔄 Loading model...")
|
|
|
|
| 19 |
n_gpu_layers=0,
|
| 20 |
verbose=False,
|
| 21 |
)
|
| 22 |
+
print("✅ Model ready!")
|
| 23 |
|
| 24 |
+
SYSTEM = """You are Pars-Medical, a bilingual (Persian/English) medical AI assistant with chain-of-thought reasoning.
|
| 25 |
+
When answering: analyze carefully, think step-by-step, provide accurate medical information.
|
| 26 |
+
Respond in the user's language. ⚠️ Educational purposes only - always consult a real doctor."""
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def chat(message, history):
|
| 30 |
+
messages = [{"role": "system", "content": SYSTEM}]
|
| 31 |
+
for user, assistant in history:
|
| 32 |
+
messages.append({"role": "user", "content": user})
|
| 33 |
+
if assistant:
|
| 34 |
+
messages.append({"role": "assistant", "content": assistant})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
messages.append({"role": "user", "content": message})
|
| 36 |
|
| 37 |
+
response = llm.create_chat_completion(messages=messages, max_tokens=512, temperature=0.7, stream=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
text = ""
|
| 40 |
for chunk in response:
|
| 41 |
+
delta = chunk["choices"][0].get("delta", {}).get("content", "")
|
| 42 |
+
text += delta
|
| 43 |
+
yield text
|
|
|
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
with gr.Blocks(title="🩺 Pars-Medical-o1", theme=gr.themes.Soft()) as demo:
|
| 47 |
gr.HTML("""
|
| 48 |
<div style="text-align:center; padding:20px;">
|
| 49 |
<h1>🩺 Pars-Medical-o1</h1>
|
| 50 |
+
<h3>دستیار پزشکی هوشمند دوزبانه | Bilingual Medical AI Assistant</h3>
|
| 51 |
+
<p><a href="https://huggingface.co/HexQuant/Pars-Medical-o1-Llama-FFT" target="_blank">🤗 Model</a> • <a href="https://twitter.com/xPOURY4" target="_blank">🐦 @xPOURY4</a> • <a href="https://github.com/xPOURY4" target="_blank">💻 GitHub</a></p>
|
|
|
|
|
|
|
|
|
|
| 52 |
</div>
|
| 53 |
""")
|
| 54 |
|
| 55 |
+
chatbot = gr.Chatbot(height=450, placeholder="سوال پزشکی بپرسید... | Ask a medical question...")
|
| 56 |
+
msg = gr.Textbox(placeholder="پیام خود را بنویسید... | Type your message...", show_label=False)
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
+
send = gr.Button("📤 ارسال | Send", variant="primary")
|
| 60 |
+
clear = gr.Button("🗑️ پاک کردن | Clear")
|
| 61 |
|
| 62 |
+
gr.Examples([
|
| 63 |
+
["علائم دیابت نوع ۲ چیست؟"],
|
| 64 |
+
["What are symptoms of hypothyroidism?"],
|
| 65 |
+
["تفاوت میگرن و سردرد تنشی چیست؟"],
|
| 66 |
+
["How to differentiate viral vs bacterial infection?"],
|
| 67 |
+
], inputs=msg, label="💡 نمونه سوالات | Examples")
|
| 68 |
|
| 69 |
gr.HTML("""
|
| 70 |
+
<div style="background:#fff3cd; padding:12px; border-radius:8px; text-align:center; margin-top:15px;">
|
| 71 |
+
⚠️ <b>توجه:</b> فقط برای آموزش است، جایگزین مشاوره پزشکی نیست<br>
|
| 72 |
+
<b>Disclaimer:</b> Educational only, not medical advice
|
| 73 |
</div>
|
| 74 |
+
<p style="text-align:center; color:#666; margin-top:15px;">Made with ❤️ by <a href="https://twitter.com/xPOURY4">@xPOURY4</a> in Iran 🇮🇷</p>
|
| 75 |
""")
|
| 76 |
|
| 77 |
+
msg.submit(chat, [msg, chatbot], chatbot).then(lambda: "", None, msg)
|
| 78 |
+
send.click(chat, [msg, chatbot], chatbot).then(lambda: "", None, msg)
|
| 79 |
clear.click(lambda: None, None, chatbot)
|
| 80 |
|
| 81 |
demo.queue().launch()
|