iamkdp commited on
Commit
b79834c
Β·
verified Β·
1 Parent(s): 2e20702

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -80
app.py CHANGED
@@ -1,29 +1,28 @@
1
  import gradio as gr
 
2
  import os
3
- import requests
4
- from cachetools import TTLCache
5
 
6
  # Configuration
7
- API_URL = "https://api.mistral.ai/v1/chat/completions"
8
- API_TOKEN = os.environ.get("HF_TOKEN") # Get from environment # Replace with your token
9
  MAX_TOKENS = 150
10
- CACHE_TTL = 3600 # 1 hour cache
11
 
12
- #Validate API key
13
- if not API_TOKEN:
14
- raise gr.Error("API_KEY environment variable not set!")
15
-
16
- # Initialize response cache
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}<s>")
39
- return f"{system_prompt}{''.join(conversation)}[INST] {message} [/INST]"
40
 
41
  def query_krishna(message, history):
42
- """Get response from Mistral API with streaming"""
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
- response = requests.post(
55
- API_URL,
56
- headers=headers,
57
- json={
58
- "inputs": formatted_prompt,
59
- "parameters": {
60
- "max_new_tokens": MAX_TOKENS,
61
- "temperature": 0.7,
62
- "top_p": 0.9,
63
- "return_full_text": False
64
- }
65
- },
66
- stream=True
67
  )
68
- response.raise_for_status()
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
- yield f"πŸ™ The divine connection is weak. Please try again. ({str(e)})"
82
 
83
  # Custom UI components
84
- krishna_avatar = "krishna.jpg" # Local file path
85
 
86
- with gr.Blocks(theme=SPIRITUAL_THEME, title="Divine Dialogue with Lord Krishna") as demo:
87
  gr.Markdown("""
88
- # πŸ•‰οΈ Bhagavad Gita Wisdom Chatbot
89
- **Ask questions to Shree Krishna** - Embodiment of Supreme Truth and Eternal Bliss
90
  """)
91
 
92
  with gr.Row():
93
  gr.Image(krishna_avatar, height=200, show_label=False)
94
 
95
- chatbot = gr.ChatInterface(
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(server_name="0.0.0.0", server_port=7860)
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