Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -2,37 +2,58 @@ from huggingface_hub import hf_hub_download
|
|
| 2 |
from llama_cpp import Llama
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
# Download
|
| 6 |
model_path = hf_hub_download(
|
| 7 |
repo_id="zeeshan391/Antobot_gguf",
|
| 8 |
filename="unsloth.Q4_K_M.gguf"
|
| 9 |
)
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
llm = Llama(
|
| 13 |
model_path=model_path,
|
| 14 |
-
n_ctx=
|
| 15 |
-
n_threads=4,
|
| 16 |
-
n_batch=64,
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Chat function
|
| 24 |
-
def
|
|
|
|
| 25 |
output = llm(prompt)
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Gradio
|
| 29 |
iface = gr.Interface(
|
| 30 |
-
fn=
|
| 31 |
-
inputs=gr.Textbox(lines=
|
| 32 |
-
outputs=gr.Textbox(label="
|
| 33 |
-
title="
|
| 34 |
-
description="
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
-
# Required for Hugging Face Spaces to expose the interface
|
| 38 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 2 |
from llama_cpp import Llama
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Download GGUF model from Hugging Face
|
| 6 |
model_path = hf_hub_download(
|
| 7 |
repo_id="zeeshan391/Antobot_gguf",
|
| 8 |
filename="unsloth.Q4_K_M.gguf"
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# Load model
|
| 12 |
llm = Llama(
|
| 13 |
model_path=model_path,
|
| 14 |
+
n_ctx=2048,
|
| 15 |
+
n_threads=4,
|
| 16 |
+
n_batch=64,
|
| 17 |
+
temperature=0.7,
|
| 18 |
+
use_mlock=True,
|
| 19 |
+
use_mmap=True,
|
| 20 |
+
verbose=False
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# Initialize chat history
|
| 24 |
+
chat_history = []
|
| 25 |
+
|
| 26 |
+
# Prompt template
|
| 27 |
+
def build_prompt(question):
|
| 28 |
+
history_text = "\n".join([f"User: {q}\nScoopsie: {a}" for q, a in chat_history[-5:]])
|
| 29 |
+
return f"""
|
| 30 |
+
You are an Anatomy assistant chatbot named "Scoopsie". Your expertise is
|
| 31 |
+
exclusively in providing information and advice about anything related to
|
| 32 |
+
medical Anatomy book topics. You do not provide information outside of this
|
| 33 |
+
scope. If a question is not about Anatomy, respond with, "I specialize only in Anatomy related queries."
|
| 34 |
+
|
| 35 |
+
Chat History: {history_text}
|
| 36 |
+
Question: {question}
|
| 37 |
+
Answer:"""
|
| 38 |
+
|
| 39 |
# Chat function
|
| 40 |
+
def chat_fn(question):
|
| 41 |
+
prompt = build_prompt(question.strip())
|
| 42 |
output = llm(prompt)
|
| 43 |
+
answer = output["choices"][0]["text"].strip()
|
| 44 |
+
|
| 45 |
+
# Save to memory
|
| 46 |
+
chat_history.append((question.strip(), answer))
|
| 47 |
+
return answer
|
| 48 |
|
| 49 |
+
# Gradio Interface
|
| 50 |
iface = gr.Interface(
|
| 51 |
+
fn=chat_fn,
|
| 52 |
+
inputs=gr.Textbox(lines=3, label="Ask questions"),
|
| 53 |
+
outputs=gr.Textbox(label="Answer"),
|
| 54 |
+
title="🧠 Human Anatomy Assistant",
|
| 55 |
+
description="Chatbot, Your anatomy expert. Only answers anatomy-related questions.",
|
| 56 |
+
theme="default"
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|