Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,28 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
-
from cachetools import TTLCache
|
| 5 |
|
| 6 |
# Configuration
|
| 7 |
-
|
| 8 |
-
|
| 9 |
MAX_TOKENS = 150
|
| 10 |
-
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
response_cache = TTLCache(maxsize=100, ttl=CACHE_TTL)
|
| 18 |
-
|
| 19 |
-
#theme
|
| 20 |
-
SPIRITUAL_THEME = gr.themes.Default(
|
| 21 |
-
primary_hue="teal", # Closest to emerald
|
| 22 |
-
secondary_hue="yellow", # Closest to gold
|
| 23 |
-
font=[gr.themes.GoogleFont("Hind Siliguri")]
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def format_krishna_prompt(message, history):
|
| 29 |
"""Create proper Mistral instruction prompt with Krishna context"""
|
|
@@ -35,88 +34,46 @@ Maintain a compassionate, all-knowing tone.
|
|
| 35 |
|
| 36 |
conversation = []
|
| 37 |
for human, assistant in history:
|
| 38 |
-
conversation.append(f"{human}[/INST] {assistant}
|
| 39 |
-
return f"{system_prompt}{''.join(conversation)}[INST] {message} [/INST]"
|
| 40 |
|
| 41 |
def query_krishna(message, history):
|
| 42 |
-
"""Get response from Mistral
|
| 43 |
-
# Check cache first
|
| 44 |
-
cache_key = (message, tuple(history))
|
| 45 |
-
if cache_key in response_cache:
|
| 46 |
-
yield response_cache[cache_key]
|
| 47 |
-
return
|
| 48 |
-
|
| 49 |
-
# Build API request
|
| 50 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 51 |
-
formatted_prompt = format_krishna_prompt(message, history)
|
| 52 |
-
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
"temperature": 0.7,
|
| 62 |
-
"top_p": 0.9,
|
| 63 |
-
"return_full_text": False
|
| 64 |
-
}
|
| 65 |
-
},
|
| 66 |
-
stream=True
|
| 67 |
)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# Stream response
|
| 71 |
-
full_response = ""
|
| 72 |
-
for chunk in response.iter_content(chunk_size=128):
|
| 73 |
-
if chunk:
|
| 74 |
-
full_response += chunk.decode("utf-8")
|
| 75 |
-
yield full_response.split("[/INST]")[-1].strip()
|
| 76 |
-
|
| 77 |
-
# Cache final response
|
| 78 |
-
response_cache[cache_key] = full_response.split("[/INST]")[-1].strip()
|
| 79 |
-
|
| 80 |
except Exception as e:
|
| 81 |
-
|
| 82 |
|
| 83 |
# Custom UI components
|
| 84 |
-
krishna_avatar = "krishna.jpg" #
|
| 85 |
|
| 86 |
-
with gr.Blocks(
|
| 87 |
gr.Markdown("""
|
| 88 |
-
# ποΈ Bhagavad Gita Wisdom Chatbot
|
| 89 |
-
**Ask questions to Shree Krishna** -
|
| 90 |
""")
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
gr.Image(krishna_avatar, height=200, show_label=False)
|
| 94 |
|
| 95 |
-
|
| 96 |
query_krishna,
|
| 97 |
-
chatbot=gr.Chatbot(
|
| 98 |
-
height=500,
|
| 99 |
-
type="messages"
|
| 100 |
-
),
|
| 101 |
-
textbox=gr.Textbox(
|
| 102 |
-
placeholder="Ask your spiritual question...",
|
| 103 |
-
container=False,
|
| 104 |
-
scale=7
|
| 105 |
-
),
|
| 106 |
examples=[
|
| 107 |
"What is the purpose of life?",
|
| 108 |
"How to achieve inner peace?",
|
| 109 |
"Explain karma yoga from Bhagavad Gita"
|
| 110 |
-
]
|
| 111 |
-
theme="compact"
|
| 112 |
)
|
| 113 |
-
|
| 114 |
-
# Footer
|
| 115 |
-
gr.Markdown("""
|
| 116 |
-
---
|
| 117 |
-
*Built with π using [Mistral-7B-Instruct](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.3) and [Gradio](https://gradio.app)*
|
| 118 |
-
""")
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
-
demo.launch(
|
| 122 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
import os
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
|
|
|
| 5 |
|
| 6 |
# Configuration
|
| 7 |
+
MODEL_REPO = "TheBloke/Mistral-7B-Instruct-v0.3-GGUF"
|
| 8 |
+
MODEL_FILE = "mistral-7b-instruct-v0.3.Q4_K_M.gguf"
|
| 9 |
MAX_TOKENS = 150
|
| 10 |
+
CPU_THREADS = os.cpu_count() or 4 # Use all available cores
|
| 11 |
|
| 12 |
+
# Download model (will cache after first run)
|
| 13 |
+
model_path = hf_hub_download(
|
| 14 |
+
repo_id=MODEL_REPO,
|
| 15 |
+
filename=MODEL_FILE,
|
| 16 |
+
cache_dir="models"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# Initialize Llama
|
| 20 |
+
llm = Llama(
|
| 21 |
+
model_path=model_path,
|
| 22 |
+
n_ctx=2048,
|
| 23 |
+
n_threads=CPU_THREADS,
|
| 24 |
+
n_gpu_layers=0 # CPU only
|
| 25 |
+
)
|
| 26 |
|
| 27 |
def format_krishna_prompt(message, history):
|
| 28 |
"""Create proper Mistral instruction prompt with Krishna context"""
|
|
|
|
| 34 |
|
| 35 |
conversation = []
|
| 36 |
for human, assistant in history:
|
| 37 |
+
conversation.append(f"{human}[/INST] {assistant}</s>")
|
| 38 |
+
return f"{system_prompt}{''.join(conversation)}<s>[INST] {message} [/INST]"
|
| 39 |
|
| 40 |
def query_krishna(message, history):
|
| 41 |
+
"""Get response from Mistral 7B on CPU"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
+
prompt = format_krishna_prompt(message, history)
|
| 44 |
+
output = llm(
|
| 45 |
+
prompt,
|
| 46 |
+
max_tokens=MAX_TOKENS,
|
| 47 |
+
temperature=0.7,
|
| 48 |
+
top_p=0.9,
|
| 49 |
+
stop=["</s>"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
+
return output['choices'][0]['text'].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
+
return f"π Divine wisdom temporarily obscured: {str(e)}"
|
| 54 |
|
| 55 |
# Custom UI components
|
| 56 |
+
krishna_avatar = "krishna.jpg" # Make sure this file is in your Space repo
|
| 57 |
|
| 58 |
+
with gr.Blocks(title="Divine Dialogue with Lord Krishna") as demo:
|
| 59 |
gr.Markdown("""
|
| 60 |
+
# ποΈ Bhagavad Gita Wisdom Chatbot (CPU Edition)
|
| 61 |
+
**Ask questions to Shree Krishna** - Running Mistral 7B Instruct on CPU
|
| 62 |
""")
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
gr.Image(krishna_avatar, height=200, show_label=False)
|
| 66 |
|
| 67 |
+
gr.ChatInterface(
|
| 68 |
query_krishna,
|
| 69 |
+
chatbot=gr.Chatbot(height=500),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
examples=[
|
| 71 |
"What is the purpose of life?",
|
| 72 |
"How to achieve inner peace?",
|
| 73 |
"Explain karma yoga from Bhagavad Gita"
|
| 74 |
+
]
|
|
|
|
| 75 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
demo.launch()
|
| 79 |
|