zsolnai commited on
Commit
10da12a
·
1 Parent(s): a4dbc3f

Add oscar gradio

Browse files
Files changed (1) hide show
  1. app.py +95 -23
app.py CHANGED
@@ -1,21 +1,21 @@
1
  import os
 
2
  import tempfile
3
 
4
  import gradio as gr
5
  import numpy as np
6
  import soundfile as sf
7
  import torch
 
8
  from huggingface_hub import hf_hub_download
 
9
  from transformers import pipeline
10
  from TTS.api import TTS
11
 
12
- from llama_cpp import Llama
13
-
14
-
15
  # --- Device Setup ---
16
- device = "cpu"
17
 
18
- # --- 1. STT Setup (Whisper) ---
19
  print("Loading Whisper...")
20
  STT_MODEL_NAME = "openai/whisper-tiny.en"
21
  stt_pipe = pipeline("automatic-speech-recognition", model=STT_MODEL_NAME, device=device)
@@ -47,13 +47,49 @@ tts_model = TTS(model_name=TTS_MODEL_NAME, progress_bar=False)
47
 
48
 
49
  # --- Core Functions ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
 
52
  def chat_with_bot(message, history):
53
- """
54
- Chat with your model using HuggingFace InferenceClient.
55
- """
56
- # Ensure history is a list
57
  if history is None:
58
  history = []
59
 
@@ -61,32 +97,67 @@ def chat_with_bot(message, history):
61
  return history, ""
62
 
63
  try:
 
 
64
  # Build conversation context from history
65
- context = ""
66
  for h in history:
67
  role = "User" if h.get("role") == "user" else "Assistant"
68
- context += f"{role}: {h.get('content', '')}\n"
69
-
70
- # Create prompt with context
71
- prompt = context + f"User: {message}\nAssistant:"
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  print(f"Generating response with Llama...")
74
-
75
- # Generate response using llama.cpp
76
  response = llm(
77
  prompt,
78
- max_tokens=256,
79
  temperature=0.7,
80
  top_p=0.95,
 
81
  )
82
-
83
  response_str = response["choices"][0]["text"].strip()
84
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  if not response_str:
86
  response_str = "I received an empty response. Please try again."
87
  print("Warning: Empty response from LLM")
88
 
89
- # Append to history
90
  history.append({"role": "user", "content": message})
91
  history.append({"role": "assistant", "content": response_str})
92
 
@@ -94,12 +165,13 @@ def chat_with_bot(message, history):
94
 
95
  except Exception as e:
96
  import traceback
 
97
  error_trace = traceback.format_exc()
98
  print(f"LLM Error: {e}")
99
  print(f"Full traceback:\n{error_trace}")
100
-
101
  error_msg = f"Error generating response: {str(e) if str(e) else 'Unknown error occurred'}"
102
-
103
  history.append({"role": "user", "content": message})
104
  history.append({"role": "assistant", "content": error_msg})
105
  return history, error_msg
@@ -241,4 +313,4 @@ with gr.Blocks() as demo:
241
  )
242
  clear_btn.click(lambda: [], None, chatbot)
243
 
244
- demo.launch(css=custom_css)
 
1
  import os
2
+ import re
3
  import tempfile
4
 
5
  import gradio as gr
6
  import numpy as np
7
  import soundfile as sf
8
  import torch
9
+ from ddgs import DDGS
10
  from huggingface_hub import hf_hub_download
11
+ from llama_cpp import Llama
12
  from transformers import pipeline
13
  from TTS.api import TTS
14
 
 
 
 
15
  # --- Device Setup ---
16
+ device = "cpu"
17
 
18
+ # --- 1. STT Setup (Whisper) ---
19
  print("Loading Whisper...")
20
  STT_MODEL_NAME = "openai/whisper-tiny.en"
21
  stt_pipe = pipeline("automatic-speech-recognition", model=STT_MODEL_NAME, device=device)
 
47
 
48
 
49
  # --- Core Functions ---
50
+ def get_web_context(message):
51
+ search_keywords = [
52
+ "current",
53
+ "latest",
54
+ "recent",
55
+ "today",
56
+ "now",
57
+ "news",
58
+ "weather",
59
+ "price",
60
+ "2024",
61
+ "2025",
62
+ "what is happening",
63
+ "score",
64
+ "match",
65
+ ]
66
+
67
+ if not any(keyword in message.lower() for keyword in search_keywords):
68
+ return None
69
+
70
+ try:
71
+ with DDGS() as ddgs:
72
+ results = list(ddgs.text(message, max_results=3))
73
+
74
+ if not results:
75
+ print("No search results found")
76
+ return None
77
+
78
+ print(f"Found {len(results)} results:")
79
+ context = "Current information from web search:\n"
80
+ for i, result in enumerate(results):
81
+ print(f"Result {i+1}: {result['title']}")
82
+ print(f" Body: {result['body'][:100]}...")
83
+ context += f"- {result['title']}: {result['body'][:200]}...\n"
84
+
85
+ return context
86
+
87
+ except Exception as e:
88
+ print(f"Search error: {e}")
89
+ return None
90
 
91
 
92
  def chat_with_bot(message, history):
 
 
 
 
93
  if history is None:
94
  history = []
95
 
 
97
  return history, ""
98
 
99
  try:
100
+ web_context = get_web_context(message=message)
101
+
102
  # Build conversation context from history
103
+ conversation = ""
104
  for h in history:
105
  role = "User" if h.get("role") == "user" else "Assistant"
106
+ conversation += f"{role}: {h.get('content', '')}\n"
107
+
108
+ # Create a clearer prompt with system instruction
109
+ if web_context:
110
+ prompt = f"""Answer ONLY using this information:
111
+
112
+ {web_context}
113
+
114
+ Question: {message}
115
+ Answer:"""
116
+ print("The web context has been added to the prompt")
117
+ else:
118
+ prompt = f"""You are a helpful assistant. Answer naturally and conversationally.
119
+ {conversation}User: {message}
120
+ Assistant:"""
121
 
122
  print(f"Generating response with Llama...")
123
+
124
+ # Generate response with stricter settings
125
  response = llm(
126
  prompt,
127
+ max_tokens=200,
128
  temperature=0.7,
129
  top_p=0.95,
130
+ stop=["User:", "\nUser:"],
131
  )
132
+
133
  response_str = response["choices"][0]["text"].strip()
134
+
135
+ response_str = response_str.strip("'\"")
136
+ response_str = response_str.rstrip(",:;")
137
+ response_str = response_str.strip("'\"")
138
+ response_str = re.sub(r"(\d+\.){10,}", "", response_str)
139
+
140
+ if "User:" in response_str:
141
+ response_str = response_str.split("User:")[0].strip()
142
+
143
+ response_str = response_str.replace("[{", "").replace("}]", "")
144
+ response_str = response_str.replace("'text':", "").replace('"text":', "")
145
+ response_str = response_str.replace("'type': 'text'", "").replace(
146
+ '"type": "text"', ""
147
+ )
148
+
149
+ if ", 'type'" in response_str or ', "type"' in response_str:
150
+ response_str = (
151
+ response_str.split(", 'type'")[0].split(', "type"')[0].strip()
152
+ )
153
+
154
+ # One final strip
155
+ response_str = response_str.strip("'\",:;")
156
+
157
  if not response_str:
158
  response_str = "I received an empty response. Please try again."
159
  print("Warning: Empty response from LLM")
160
 
 
161
  history.append({"role": "user", "content": message})
162
  history.append({"role": "assistant", "content": response_str})
163
 
 
165
 
166
  except Exception as e:
167
  import traceback
168
+
169
  error_trace = traceback.format_exc()
170
  print(f"LLM Error: {e}")
171
  print(f"Full traceback:\n{error_trace}")
172
+
173
  error_msg = f"Error generating response: {str(e) if str(e) else 'Unknown error occurred'}"
174
+
175
  history.append({"role": "user", "content": message})
176
  history.append({"role": "assistant", "content": error_msg})
177
  return history, error_msg
 
313
  )
314
  clear_btn.click(lambda: [], None, chatbot)
315
 
316
+ demo.launch()