File size: 2,445 Bytes
588715d b79834c 10359d9 b79834c 588715d f14975e 588715d f14975e b79834c f14975e 588715d f14975e b79834c 9c6953b 602df1f b79834c 588715d 602df1f b79834c 602df1f b79834c 602df1f b79834c 602df1f b79834c 588715d 602df1f b79834c 588715d b79834c 602df1f f4e2b21 602df1f b79834c 602df1f b79834c 602df1f b79834c 602df1f 588715d b79834c 602df1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import gradio as gr
from llama_cpp import Llama
import os
from huggingface_hub import hf_hub_download
# Verified working repositories (as of July 2024)
MODEL_REPO = "bartowski/Mistral-7B-Instruct-v0.3-GGUF"
MODEL_FILE = "Mistral-7B-Instruct-v0.3-Q4_K_M.gguf"
# Download model
model_path = hf_hub_download(
repo_id=MODEL_REPO,
filename=MODEL_FILE,
token=os.environ.get("HF_TOKEN") # Required for gated models
)
# Configuration
MODEL_REPO = "TheBloke/Mistral-7B-Instruct-v0.3-GGUF"
MODEL_FILE = "mistral-7b-instruct-v0.3.Q4_K_M.gguf"
MAX_TOKENS = 150
CPU_THREADS = os.cpu_count()
# Initialize Llama
llm = Llama(
model_path=model_path,
n_ctx=2048,
n_threads=CPU_THREADS,
n_gpu_layers=0 # CPU only
)
def format_krishna_prompt(message, history):
"""Create proper Mistral instruction prompt with Krishna context"""
system_prompt = """<s>[INST] <<SYS>>
You are Lord Krishna, the divine charioteer of Arjuna in Bhagavad Gita.
Answer with Vedic wisdom using simple English and occasional Sanskrit terms.
Maintain a compassionate, all-knowing tone.
<</SYS>>"""
conversation = []
for human, assistant in history:
conversation.append(f"{human}[/INST] {assistant}</s>")
return f"{system_prompt}{''.join(conversation)}<s>[INST] {message} [/INST]"
def query_krishna(message, history):
"""Get response from Mistral 7B on CPU"""
try:
prompt = format_krishna_prompt(message, history)
output = llm(
prompt,
max_tokens=MAX_TOKENS,
temperature=0.7,
top_p=0.9,
stop=["</s>"]
)
return output['choices'][0]['text'].strip()
except Exception as e:
return f"π Divine wisdom temporarily obscured: {str(e)}"
# Custom UI components
krishna_avatar = "krishna.jpg" # Make sure this file is in your Space repo
with gr.Blocks(title="Divine Dialogue with Lord Krishna") as demo:
gr.Markdown("""
# ποΈ Bhagavad Gita Wisdom Chatbot
**Ask questions to Shree Krishna**
""")
with gr.Row():
gr.Image(krishna_avatar, height=200, show_label=False)
gr.ChatInterface(
query_krishna,
chatbot=gr.Chatbot(height=500),
examples=[
"What is the purpose of life?",
"How to achieve inner peace?",
"Explain karma yoga from Bhagavad Gita"
]
)
if __name__ == "__main__":
demo.launch()
|