Abdullah7485 commited on
Commit
ae0102f
·
verified ·
1 Parent(s): b4c05d3
Files changed (1) hide show
  1. app.py +39 -18
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 the GGUF model from Hugging Face Hub
6
  model_path = hf_hub_download(
7
  repo_id="zeeshan391/Antobot_gguf",
8
  filename="unsloth.Q4_K_M.gguf"
9
  )
10
 
11
- # Load the model with optimized settings for CPU
12
  llm = Llama(
13
  model_path=model_path,
14
- n_ctx=1024, # Reduced context window to improve speed
15
- n_threads=4, # Optimal for limited CPU cores
16
- n_batch=64, # Batch size for token generation
17
- use_mlock=True, # Prevents swapping, faster on Spaces
18
- use_mmap=True, # Memory-map model file for lower RAM use
19
- verbose=False, # Disable debug logs for cleaner output
20
- bos_token="<s>" # Optional: define BOS token
21
  )
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Chat function
24
- def chat(prompt):
 
25
  output = llm(prompt)
26
- return output['choices'][0]['text'].strip()
 
 
 
 
27
 
28
- # Gradio UI
29
  iface = gr.Interface(
30
- fn=chat,
31
- inputs=gr.Textbox(lines=4, label="Ask Antobot"),
32
- outputs=gr.Textbox(label="Antobot's Answer"),
33
- title="🤖 Human Anatomy Assistant",
34
- description="You Can Ask Questions About Human Anatomy."
 
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)