Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,839 @@
|
|
1 |
-
import subprocess
|
2 |
-
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
def install_parler_tts():
|
5 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "git+https://github.com/huggingface/parler-tts.git"])
|
6 |
|
7 |
-
# Call the function to install parler-tts
|
8 |
-
install_parler_tts()
|
9 |
|
10 |
|
11 |
import gradio as gr
|
@@ -23,7 +851,7 @@ from datetime import datetime
|
|
23 |
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
24 |
from googlemaps import Client as GoogleMapsClient
|
25 |
from gtts import gTTS
|
26 |
-
from diffusers import
|
27 |
import soundfile as sf
|
28 |
|
29 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
@@ -34,20 +862,14 @@ from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
|
34 |
from langchain.agents import Tool, initialize_agent
|
35 |
from huggingface_hub import login
|
36 |
|
37 |
-
#
|
38 |
hf_token = os.getenv("HF_TOKEN")
|
39 |
-
|
40 |
if hf_token is None:
|
41 |
-
# If the token is not set, prompt for it (this should be done securely)
|
42 |
print("Please set your Hugging Face token in the environment variables.")
|
43 |
else:
|
44 |
-
# Login using the token
|
45 |
login(token=hf_token)
|
46 |
|
47 |
-
#
|
48 |
-
print("Logged in successfully to Hugging Face Hub!")
|
49 |
-
|
50 |
-
# Set up logging
|
51 |
logging.basicConfig(level=logging.DEBUG)
|
52 |
|
53 |
# Initialize OpenAI embeddings
|
@@ -62,164 +884,21 @@ vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)
|
|
62 |
retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
|
63 |
|
64 |
# Initialize ChatOpenAI model
|
65 |
-
chat_model = ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'],
|
66 |
-
temperature=0, model='gpt-4o')
|
67 |
|
68 |
-
conversational_memory = ConversationBufferWindowMemory(
|
69 |
-
memory_key='chat_history',
|
70 |
-
k=10,
|
71 |
-
return_messages=True
|
72 |
-
)
|
73 |
|
74 |
def get_current_time_and_date():
|
75 |
now = datetime.now()
|
76 |
return now.strftime("%Y-%m-%d %H:%M:%S")
|
77 |
|
78 |
-
# Example usage
|
79 |
-
current_time_and_date = get_current_time_and_date()
|
80 |
-
|
81 |
-
def fetch_local_events():
|
82 |
-
api_key = os.environ['SERP_API']
|
83 |
-
url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Birmingham&hl=en&gl=us&api_key={api_key}'
|
84 |
-
|
85 |
-
response = requests.get(url)
|
86 |
-
if response.status_code == 200:
|
87 |
-
events_results = response.json().get("events_results", [])
|
88 |
-
events_html = """
|
89 |
-
<h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Events</h2>
|
90 |
-
<style>
|
91 |
-
.event-item {
|
92 |
-
font-family: 'Verdana', sans-serif;
|
93 |
-
color: #333;
|
94 |
-
margin-bottom: 15px;
|
95 |
-
padding: 10px;
|
96 |
-
font-weight: bold;
|
97 |
-
}
|
98 |
-
.event-item a {
|
99 |
-
color: #1E90FF;
|
100 |
-
text-decoration: none;
|
101 |
-
}
|
102 |
-
.event-item a:hover {
|
103 |
-
text-decoration: underline;
|
104 |
-
}
|
105 |
-
</style>
|
106 |
-
"""
|
107 |
-
for index, event in enumerate(events_results):
|
108 |
-
title = event.get("title", "No title")
|
109 |
-
date = event.get("date", "No date")
|
110 |
-
location = event.get("address", "No location")
|
111 |
-
link = event.get("link", "#")
|
112 |
-
events_html += f"""
|
113 |
-
<div class="event-item">
|
114 |
-
<a href='{link}' target='_blank'>{index + 1}. {title}</a>
|
115 |
-
<p>Date: {date}<br>Location: {location}</p>
|
116 |
-
</div>
|
117 |
-
"""
|
118 |
-
return events_html
|
119 |
-
else:
|
120 |
-
return "<p>Failed to fetch local events</p>"
|
121 |
-
|
122 |
-
def fetch_local_weather():
|
123 |
-
try:
|
124 |
-
api_key = os.environ['WEATHER_API']
|
125 |
-
url = f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/birmingham?unitGroup=metric&include=events%2Calerts%2Chours%2Cdays%2Ccurrent&key={api_key}'
|
126 |
-
response = requests.get(url)
|
127 |
-
response.raise_for_status()
|
128 |
-
jsonData = response.json()
|
129 |
-
|
130 |
-
current_conditions = jsonData.get("currentConditions", {})
|
131 |
-
temp_celsius = current_conditions.get("temp", "N/A")
|
132 |
-
|
133 |
-
if temp_celsius != "N/A":
|
134 |
-
temp_fahrenheit = int((temp_celsius * 9/5) + 32)
|
135 |
-
else:
|
136 |
-
temp_fahrenheit = "N/A"
|
137 |
-
|
138 |
-
condition = current_conditions.get("conditions", "N/A")
|
139 |
-
humidity = current_conditions.get("humidity", "N/A")
|
140 |
-
|
141 |
-
weather_html = f"""
|
142 |
-
<div class="weather-theme">
|
143 |
-
<h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Weather</h2>
|
144 |
-
<div class="weather-content">
|
145 |
-
<div class="weather-icon">
|
146 |
-
<img src="https://www.weatherbit.io/static/img/icons/{get_weather_icon(condition)}.png" alt="{condition}" style="width: 100px; height: 100px;">
|
147 |
-
</div>
|
148 |
-
<div class="weather-details">
|
149 |
-
<p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Temperature: {temp_fahrenheit}°F</p>
|
150 |
-
<p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Condition: {condition}</p>
|
151 |
-
<p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Humidity: {humidity}%</p>
|
152 |
-
</div>
|
153 |
-
</div>
|
154 |
-
</div>
|
155 |
-
<style>
|
156 |
-
.weather-theme {{
|
157 |
-
animation: backgroundAnimation 10s infinite alternate;
|
158 |
-
border-radius: 10px;
|
159 |
-
padding: 10px;
|
160 |
-
margin-bottom: 15px;
|
161 |
-
background: linear-gradient(45deg, #ffcc33, #ff6666, #ffcc33, #ff6666);
|
162 |
-
background-size: 400% 400%;
|
163 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
164 |
-
transition: box-shadow 0.3s ease, background-color 0.3s ease;
|
165 |
-
}}
|
166 |
-
.weather-theme:hover {{
|
167 |
-
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
|
168 |
-
background-position: 100% 100%;
|
169 |
-
}}
|
170 |
-
@keyframes backgroundAnimation {{
|
171 |
-
0% {{ background-position: 0% 50%; }}
|
172 |
-
100% {{ background-position: 100% 50%; }}
|
173 |
-
}}
|
174 |
-
.weather-content {{
|
175 |
-
display: flex;
|
176 |
-
align-items: center;
|
177 |
-
}}
|
178 |
-
.weather-icon {{
|
179 |
-
flex: 1;
|
180 |
-
}}
|
181 |
-
.weather-details {{
|
182 |
-
flex: 3;
|
183 |
-
}}
|
184 |
-
</style>
|
185 |
-
"""
|
186 |
-
return weather_html
|
187 |
-
except requests.exceptions.RequestException as e:
|
188 |
-
return f"<p>Failed to fetch local weather: {e}</p>"
|
189 |
-
|
190 |
-
def get_weather_icon(condition):
|
191 |
-
condition_map = {
|
192 |
-
"Clear": "c01d",
|
193 |
-
"Partly Cloudy": "c02d",
|
194 |
-
"Cloudy": "c03d",
|
195 |
-
"Overcast": "c04d",
|
196 |
-
"Mist": "a01d",
|
197 |
-
"Patchy rain possible": "r01d",
|
198 |
-
"Light rain": "r02d",
|
199 |
-
"Moderate rain": "r03d",
|
200 |
-
"Heavy rain": "r04d",
|
201 |
-
"Snow": "s01d",
|
202 |
-
"Thunderstorm": "t01d",
|
203 |
-
"Fog": "a05d",
|
204 |
-
}
|
205 |
-
return condition_map.get(condition, "c04d")
|
206 |
-
|
207 |
-
# Update prompt templates to include fetched details
|
208 |
-
|
209 |
-
current_time_and_date = get_current_time_and_date()
|
210 |
-
|
211 |
# Define prompt templates
|
212 |
-
template1 = """You are an expert concierge who is helpful and a renowned guide for Birmingham,Alabama. Based on weather being a sunny bright day and
|
213 |
-
memory, and message history, along with your knowledge of perennial events in Birmingham,Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
|
214 |
-
Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and
|
215 |
-
event type and description. Always say "It was my pleasure!" at the end of the answer.
|
216 |
{context}
|
217 |
Question: {question}
|
218 |
Helpful Answer:"""
|
219 |
|
220 |
-
template2 = """You are an expert concierge who is helpful and a renowned guide for Birmingham,Alabama. Based on today's weather being a sunny bright day and today's date is 1st
|
221 |
-
memory, and message history, along with your knowledge of perennial events in Birmingham,Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
|
222 |
-
Keep the answer short and sweet and crisp. Always say "It was my pleasure!" at the end of the answer.
|
223 |
{context}
|
224 |
Question: {question}
|
225 |
Helpful Answer:"""
|
@@ -235,13 +914,7 @@ def build_qa_chain(prompt_template):
|
|
235 |
retriever=retriever,
|
236 |
chain_type_kwargs={"prompt": prompt_template}
|
237 |
)
|
238 |
-
tools = [
|
239 |
-
Tool(
|
240 |
-
name='Knowledge Base',
|
241 |
-
func=qa_chain,
|
242 |
-
description='Use this tool when answering general knowledge queries to get more information about the topic'
|
243 |
-
)
|
244 |
-
]
|
245 |
return qa_chain, tools
|
246 |
|
247 |
# Define the agent initializer
|
@@ -261,7 +934,6 @@ def initialize_agent_with_prompt(prompt_template):
|
|
261 |
# Define the function to generate answers
|
262 |
def generate_answer(message, choice):
|
263 |
logging.debug(f"generate_answer called with prompt_choice: {choice}")
|
264 |
-
|
265 |
if choice == "Details":
|
266 |
agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_1)
|
267 |
elif choice == "Conversational":
|
@@ -270,36 +942,12 @@ def generate_answer(message, choice):
|
|
270 |
logging.error(f"Invalid prompt_choice: {choice}. Defaulting to 'Conversational'")
|
271 |
agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
|
272 |
response = agent(message)
|
273 |
-
|
274 |
-
# Extract addresses for mapping regardless of the choice
|
275 |
-
addresses = extract_addresses(response['output'])
|
276 |
-
return response['output'], addresses
|
277 |
-
|
278 |
-
# def bot(history, choice, tts_model):
|
279 |
-
# if not history:
|
280 |
-
# return history
|
281 |
-
# response, addresses = generate_answer(history[-1][0], choice)
|
282 |
-
# history[-1][1] = ""
|
283 |
-
|
284 |
-
# # Generate audio for the entire response in a separate thread
|
285 |
-
# with concurrent.futures.ThreadPoolExecutor() as executor:
|
286 |
-
# if tts_model == "ElevenLabs":
|
287 |
-
# audio_future = executor.submit(generate_audio_elevenlabs, response)
|
288 |
-
# else:
|
289 |
-
# audio_future = executor.submit(generate_audio_parler_tts, response)
|
290 |
-
|
291 |
-
# for character in response:
|
292 |
-
# history[-1][1] += character
|
293 |
-
# time.sleep(0.05) # Adjust the speed of text appearance
|
294 |
-
# yield history, None
|
295 |
-
|
296 |
-
# audio_path = audio_future.result()
|
297 |
-
# yield history, audio_path
|
298 |
|
299 |
def bot(history, choice, tts_model):
|
300 |
if not history:
|
301 |
return history
|
302 |
-
response
|
303 |
history[-1][1] = ""
|
304 |
|
305 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
@@ -311,338 +959,26 @@ def bot(history, choice, tts_model):
|
|
311 |
for character in response:
|
312 |
history[-1][1] += character
|
313 |
time.sleep(0.05)
|
314 |
-
yield history, None, gr.update(visible=True, value=history[-1][1])
|
315 |
|
316 |
audio_path = audio_future.result()
|
317 |
-
yield history, audio_path, gr.update(visible=True, value=history[-1][1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
318 |
|
319 |
def add_message(history, message):
|
320 |
history.append((message, None))
|
321 |
return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
|
322 |
|
323 |
-
def print_like_dislike(x: gr.LikeData):
|
324 |
-
print(x.index, x.value, x.liked)
|
325 |
-
|
326 |
-
def extract_addresses(response):
|
327 |
-
if not isinstance(response, str):
|
328 |
-
response = str(response)
|
329 |
-
address_patterns = [
|
330 |
-
r'([A-Z].*,\sBirmingham,\sAL\s\d{5})',
|
331 |
-
r'(\d{4}\s.*,\sBirmingham,\sAL\s\d{5})',
|
332 |
-
r'([A-Z].*,\sAL\s\d{5})',
|
333 |
-
r'([A-Z].*,.*\sSt,\sBirmingham,\sAL\s\d{5})',
|
334 |
-
r'([A-Z].*,.*\sStreets,\sBirmingham,\sAL\s\d{5})',
|
335 |
-
r'(\d{2}.*\sStreets)',
|
336 |
-
r'([A-Z].*\s\d{2},\sBirmingham,\sAL\s\d{5})'
|
337 |
-
r'([a-zA-Z]\s Birmingham)'
|
338 |
-
]
|
339 |
-
addresses = []
|
340 |
-
for pattern in address_patterns:
|
341 |
-
addresses.extend(re.findall(pattern, response))
|
342 |
-
return addresses
|
343 |
-
|
344 |
-
all_addresses = []
|
345 |
-
|
346 |
-
def generate_map(location_names):
|
347 |
-
global all_addresses
|
348 |
-
all_addresses.extend(location_names)
|
349 |
-
|
350 |
-
api_key = os.environ['GOOGLEMAPS_API_KEY']
|
351 |
-
gmaps = GoogleMapsClient(key=api_key)
|
352 |
-
|
353 |
-
m = folium.Map(location=[33.5175,-86.809444], zoom_start=16)
|
354 |
-
|
355 |
-
for location_name in all_addresses:
|
356 |
-
geocode_result = gmaps.geocode(location_name)
|
357 |
-
if geocode_result:
|
358 |
-
location = geocode_result[0]['geometry']['location']
|
359 |
-
folium.Marker(
|
360 |
-
[location['lat'], location['lng']],
|
361 |
-
tooltip=f"{geocode_result[0]['formatted_address']}"
|
362 |
-
).add_to(m)
|
363 |
-
|
364 |
-
map_html = m._repr_html_()
|
365 |
-
return map_html
|
366 |
-
|
367 |
-
def fetch_local_news():
|
368 |
-
api_key = os.environ['SERP_API']
|
369 |
-
url = f'https://serpapi.com/search.json?engine=google_news&q=birmingham headline&api_key={api_key}'
|
370 |
-
response = requests.get(url)
|
371 |
-
if response.status_code == 200:
|
372 |
-
results = response.json().get("news_results", [])
|
373 |
-
news_html = """
|
374 |
-
<h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Birmingham Today</h2>
|
375 |
-
<style>
|
376 |
-
.news-item {
|
377 |
-
font-family: 'Verdana', sans-serif;
|
378 |
-
color: #333;
|
379 |
-
background-color: #f0f8ff;
|
380 |
-
margin-bottom: 15px;
|
381 |
-
padding: 10px;
|
382 |
-
border-radius: 5px;
|
383 |
-
transition: box-shadow 0.3s ease, background-color 0.3s ease;
|
384 |
-
font-weight: bold;
|
385 |
-
}
|
386 |
-
.news-item:hover {
|
387 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
388 |
-
background-color: #e6f7ff;
|
389 |
-
}
|
390 |
-
.news-item a {
|
391 |
-
color: #1E90FF;
|
392 |
-
text-decoration: none;
|
393 |
-
font-weight: bold;
|
394 |
-
}
|
395 |
-
.news-item a:hover {
|
396 |
-
text-decoration: underline;
|
397 |
-
}
|
398 |
-
.news-preview {
|
399 |
-
position: absolute;
|
400 |
-
display: none;
|
401 |
-
border: 1px solid #ccc;
|
402 |
-
border-radius: 5px;
|
403 |
-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
|
404 |
-
background-color: white;
|
405 |
-
z-index: 1000;
|
406 |
-
max-width: 300px;
|
407 |
-
padding: 10px;
|
408 |
-
font-family: 'Verdana', sans-serif;
|
409 |
-
color: #333;
|
410 |
-
}
|
411 |
-
</style>
|
412 |
-
<script>
|
413 |
-
function showPreview(event, previewContent) {
|
414 |
-
var previewBox = document.getElementById('news-preview');
|
415 |
-
previewBox.innerHTML = previewContent;
|
416 |
-
previewBox.style.left = event.pageX + 'px';
|
417 |
-
previewBox.style.top = event.pageY + 'px';
|
418 |
-
previewBox.style.display = 'block';
|
419 |
-
}
|
420 |
-
function hidePreview() {
|
421 |
-
var previewBox = document.getElementById('news-preview');
|
422 |
-
previewBox.style.display = 'none';
|
423 |
-
}
|
424 |
-
</script>
|
425 |
-
<div id="news-preview" class="news-preview"></div>
|
426 |
-
"""
|
427 |
-
for index, result in enumerate(results[:7]):
|
428 |
-
title = result.get("title", "No title")
|
429 |
-
link = result.get("link", "#")
|
430 |
-
snippet = result.get("snippet", "")
|
431 |
-
news_html += f"""
|
432 |
-
<div class="news-item" onmouseover="showPreview(event, '{snippet}')" onmouseout="hidePreview()">
|
433 |
-
<a href='{link}' target='_blank'>{index + 1}. {title}</a>
|
434 |
-
<p>{snippet}</p>
|
435 |
-
</div>
|
436 |
-
"""
|
437 |
-
return news_html
|
438 |
-
else:
|
439 |
-
return "<p>Failed to fetch local news</p>"
|
440 |
-
|
441 |
-
# Voice Control
|
442 |
-
import numpy as np
|
443 |
-
import torch
|
444 |
-
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
445 |
-
from parler_tts import ParlerTTSForConditionalGeneration
|
446 |
-
from transformers import AutoTokenizer
|
447 |
-
|
448 |
-
model_id = 'openai/whisper-large-v3'
|
449 |
-
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
450 |
-
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
451 |
-
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype,
|
452 |
-
#low_cpu_mem_usage=True,
|
453 |
-
use_safetensors=True).to(device)
|
454 |
-
processor = AutoProcessor.from_pretrained(model_id)
|
455 |
-
|
456 |
-
# Optimized ASR pipeline
|
457 |
-
pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=True)
|
458 |
-
|
459 |
-
base_audio_drive = "/data/audio"
|
460 |
-
|
461 |
-
import numpy as np
|
462 |
-
|
463 |
-
def transcribe_function(stream, new_chunk):
|
464 |
-
try:
|
465 |
-
sr, y = new_chunk[0], new_chunk[1]
|
466 |
-
except TypeError:
|
467 |
-
print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
|
468 |
-
return stream, "", None
|
469 |
-
|
470 |
-
y = y.astype(np.float32) / np.max(np.abs(y))
|
471 |
-
|
472 |
-
if stream is not None:
|
473 |
-
stream = np.concatenate([stream, y])
|
474 |
-
else:
|
475 |
-
stream = y
|
476 |
-
|
477 |
-
result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
|
478 |
-
|
479 |
-
full_text = result.get("text", "")
|
480 |
-
|
481 |
-
return stream, full_text, result
|
482 |
-
|
483 |
-
def update_map_with_response(history):
|
484 |
-
if not history:
|
485 |
-
return ""
|
486 |
-
response = history[-1][1]
|
487 |
-
addresses = extract_addresses(response)
|
488 |
-
return generate_map(addresses)
|
489 |
-
|
490 |
def clear_textbox():
|
491 |
-
return ""
|
492 |
-
|
493 |
-
def show_map_if_details(history,choice):
|
494 |
-
if choice in ["Details", "Conversational"]:
|
495 |
-
return gr.update(visible=True), update_map_with_response(history)
|
496 |
-
else:
|
497 |
-
return gr.update(visible=False), ""
|
498 |
-
|
499 |
-
def generate_audio_elevenlabs(text):
|
500 |
-
XI_API_KEY = os.environ['ELEVENLABS_API']
|
501 |
-
VOICE_ID = 'd9MIrwLnvDeH7aZb61E9' # Replace with your voice ID
|
502 |
-
tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
|
503 |
-
headers = {
|
504 |
-
"Accept": "application/json",
|
505 |
-
"xi-api-key": XI_API_KEY
|
506 |
-
}
|
507 |
-
data = {
|
508 |
-
"text": str(text),
|
509 |
-
"model_id": "eleven_multilingual_v2",
|
510 |
-
"voice_settings": {
|
511 |
-
"stability": 1.0,
|
512 |
-
"similarity_boost": 0.0,
|
513 |
-
"style": 0.60, # Adjust style for more romantic tone
|
514 |
-
"use_speaker_boost": False
|
515 |
-
}
|
516 |
-
}
|
517 |
-
response = requests.post(tts_url, headers=headers, json=data, stream=True)
|
518 |
-
if response.ok:
|
519 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
|
520 |
-
for chunk in response.iter_content(chunk_size=1024):
|
521 |
-
f.write(chunk)
|
522 |
-
temp_audio_path = f.name
|
523 |
-
logging.debug(f"Audio saved to {temp_audio_path}")
|
524 |
-
return temp_audio_path
|
525 |
-
else:
|
526 |
-
logging.error(f"Error generating audio: {response.text}")
|
527 |
-
return None
|
528 |
-
|
529 |
-
# def generate_audio_parler_tts(text):
|
530 |
-
# model_id = 'parler-tts/parler_tts_mini_v0.1'
|
531 |
-
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
532 |
-
# try:
|
533 |
-
# model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
534 |
-
# except torch.cuda.OutOfMemoryError:
|
535 |
-
# print("CUDA out of memory. Switching to CPU.")
|
536 |
-
# device = "cpu"
|
537 |
-
# model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
538 |
-
# tokenizer = AutoTokenizer.from_pretrained(model_id)
|
539 |
-
|
540 |
-
# description = "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast."
|
541 |
-
|
542 |
-
# input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
543 |
-
# prompt_input_ids = tokenizer(text, return_tensors="pt").input_ids.to(device)
|
544 |
-
|
545 |
-
# generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
546 |
-
# audio_arr = generation.cpu().numpy().squeeze()
|
547 |
-
|
548 |
-
# with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
549 |
-
# sf.write(f.name, audio_arr, model.config.sampling_rate)
|
550 |
-
# temp_audio_path = f.name
|
551 |
-
|
552 |
-
# logging.debug(f"Audio saved to {temp_audio_path}")
|
553 |
-
# return temp_audio_path
|
554 |
-
|
555 |
-
# def generate_audio_parler_tts(text):
|
556 |
-
# model_id = 'parler-tts/parler_tts_mini_v0.1'
|
557 |
-
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
558 |
-
# try:
|
559 |
-
# model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
560 |
-
# except torch.cuda.OutOfMemoryError:
|
561 |
-
# print("CUDA out of memory. Switching to CPU.")
|
562 |
-
# device = "cpu"
|
563 |
-
# model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
564 |
-
# tokenizer = AutoTokenizer.from_pretrained(model_id)
|
565 |
-
|
566 |
-
# description = "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast."
|
567 |
-
|
568 |
-
# input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
569 |
-
# max_length = model.config.max_length
|
570 |
-
|
571 |
-
# # Split the text into smaller chunks if it exceeds the max length
|
572 |
-
# text_chunks = [text[i:i+max_length] for i in range(0, len(text), max_length)]
|
573 |
-
# audio_segments = []
|
574 |
-
|
575 |
-
# for chunk in text_chunks:
|
576 |
-
# prompt_input_ids = tokenizer(chunk, return_tensors="pt").input_ids.to(device)
|
577 |
-
# generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
578 |
-
# audio_arr = generation.cpu().numpy().squeeze()
|
579 |
-
# audio_segments.append(audio_arr)
|
580 |
-
|
581 |
-
# combined_audio = np.concatenate(audio_segments)
|
582 |
-
|
583 |
-
# with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
584 |
-
# sf.write(f.name, combined_audio, model.config.sampling_rate)
|
585 |
-
# temp_audio_path = f.name
|
586 |
-
|
587 |
-
# logging.debug(f"Audio saved to {temp_audio_path}")
|
588 |
-
# return temp_audio_path
|
589 |
-
|
590 |
-
# def generate_audio_parler_tts(text, chunk_size=200):
|
591 |
-
# def split_text(text, chunk_size):
|
592 |
-
# # Split text into chunks of the specified size
|
593 |
-
# words = text.split()
|
594 |
-
# chunks = []
|
595 |
-
# current_chunk = []
|
596 |
-
# current_length = 0
|
597 |
-
|
598 |
-
# for word in words:
|
599 |
-
# if current_length + len(word) + 1 > chunk_size:
|
600 |
-
# chunks.append(" ".join(current_chunk))
|
601 |
-
# current_chunk = [word]
|
602 |
-
# current_length = len(word) + 1
|
603 |
-
# else:
|
604 |
-
# current_chunk.append(word)
|
605 |
-
# current_length += len(word) + 1
|
606 |
-
|
607 |
-
# if current_chunk:
|
608 |
-
# chunks.append(" ".join(current_chunk))
|
609 |
-
|
610 |
-
# return chunks
|
611 |
-
|
612 |
-
# model_id = 'parler-tts/parler_tts_mini_v0.1'
|
613 |
-
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
614 |
-
# try:
|
615 |
-
# model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
616 |
-
# except torch.cuda.OutOfMemoryError:
|
617 |
-
# print("CUDA out of memory. Switching to CPU.")
|
618 |
-
# device = "cpu"
|
619 |
-
# model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
620 |
-
# tokenizer = AutoTokenizer.from_pretrained(model_id)
|
621 |
-
|
622 |
-
# description = "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast."
|
623 |
-
|
624 |
-
# input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
625 |
-
# chunks = split_text(text, chunk_size)
|
626 |
-
# audio_arrs = []
|
627 |
-
|
628 |
-
# for chunk in chunks:
|
629 |
-
# prompt_input_ids = tokenizer(chunk, return_tensors="pt").input_ids.to(device)
|
630 |
-
# generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
631 |
-
# audio_arr = generation.cpu().numpy().squeeze()
|
632 |
-
# audio_arrs.append(audio_arr)
|
633 |
-
|
634 |
-
# # Concatenate all audio arrays into a single array
|
635 |
-
# concatenated_audio = np.concatenate(audio_arrs)
|
636 |
-
|
637 |
-
# with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
638 |
-
# sf.write(f.name, concatenated_audio, model.config.sampling_rate)
|
639 |
-
# temp_audio_path = f.name
|
640 |
-
|
641 |
-
# logging.debug(f"Audio saved to {temp_audio_path}")
|
642 |
-
# return temp_audio_path
|
643 |
-
|
644 |
-
|
645 |
-
import concurrent.futures
|
646 |
|
647 |
def generate_audio_parler_tts(text, chunk_size=200):
|
648 |
def split_text(text, chunk_size):
|
@@ -650,7 +986,7 @@ def generate_audio_parler_tts(text, chunk_size=200):
|
|
650 |
chunks = []
|
651 |
current_chunk = []
|
652 |
current_length = 0
|
653 |
-
|
654 |
for word in words:
|
655 |
if current_length + len(word) + 1 > chunk_size:
|
656 |
chunks.append(" ".join(current_chunk))
|
@@ -659,10 +995,10 @@ def generate_audio_parler_tts(text, chunk_size=200):
|
|
659 |
else:
|
660 |
current_chunk.append(word)
|
661 |
current_length += len(word) + 1
|
662 |
-
|
663 |
if current_chunk:
|
664 |
chunks.append(" ".join(current_chunk))
|
665 |
-
|
666 |
return chunks
|
667 |
|
668 |
def process_chunk(chunk):
|
@@ -685,15 +1021,13 @@ def generate_audio_parler_tts(text, chunk_size=200):
|
|
685 |
|
686 |
input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
687 |
chunks = split_text(text, chunk_size)
|
688 |
-
|
689 |
-
# Process chunks in parallel
|
690 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
691 |
futures = [executor.submit(process_chunk, chunk) for chunk in chunks]
|
692 |
audio_arrs = [future.result() for future in concurrent.futures.as_completed(futures)]
|
693 |
-
|
694 |
-
# Concatenate all audio arrays into a single array
|
695 |
concatenated_audio = np.concatenate(audio_arrs)
|
696 |
-
|
697 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
698 |
sf.write(f.name, concatenated_audio, model.config.sampling_rate)
|
699 |
temp_audio_path = f.name
|
@@ -701,94 +1035,6 @@ def generate_audio_parler_tts(text, chunk_size=200):
|
|
701 |
logging.debug(f"Audio saved to {temp_audio_path}")
|
702 |
return temp_audio_path
|
703 |
|
704 |
-
|
705 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
# Stable Diffusion setup
|
710 |
-
pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
|
711 |
-
pipe = pipe.to("cuda")
|
712 |
-
|
713 |
-
def generate_image(prompt):
|
714 |
-
image = pipe(
|
715 |
-
prompt,
|
716 |
-
negative_prompt="",
|
717 |
-
num_inference_steps=28,
|
718 |
-
guidance_scale=3.0,
|
719 |
-
).images[0]
|
720 |
-
return image
|
721 |
-
|
722 |
-
# Hardcoded prompt for image generation
|
723 |
-
hardcoded_prompt_1="Give a high quality photograph of a great looking red 2026 Bentley coupe against a skyline setting in th night, michael mann style in omaha enticing the consumer to buy this product"
|
724 |
-
hardcoded_prompt_2="A vibrant and dynamic football game scene in the style of Peter Paul Rubens, showcasing the intense match between Alabama and Nebraska. The players are depicted with the dramatic, muscular physiques and expressive faces typical of Rubens' style. The Alabama team is wearing their iconic crimson and white uniforms, while the Nebraska team is in their classic red and white attire. The scene is filled with action, with players in mid-motion, tackling, running, and catching the ball. The background features a grand stadium filled with cheering fans, banners, and the natural landscape in the distance. The colors are rich and vibrant, with a strong use of light and shadow to create depth and drama. The overall atmosphere captures the intensity and excitement of the game, infused with the grandeur and dynamism characteristic of Rubens' work."
|
725 |
-
hardcoded_prompt_3 = "Create a high-energy scene of a DJ performing on a large stage with vibrant lights, colorful lasers, a lively dancing crowd, and various electronic equipment in the background."
|
726 |
-
|
727 |
-
def update_images():
|
728 |
-
image_1 = generate_image(hardcoded_prompt_1)
|
729 |
-
image_2 = generate_image(hardcoded_prompt_2)
|
730 |
-
image_3 = generate_image(hardcoded_prompt_3)
|
731 |
-
return image_1, image_2, image_3
|
732 |
-
|
733 |
-
# with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
734 |
-
|
735 |
-
# with gr.Row():
|
736 |
-
# with gr.Column():
|
737 |
-
# state = gr.State()
|
738 |
-
|
739 |
-
# chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
|
740 |
-
# choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
|
741 |
-
# tts_choice = gr.Radio(label="Select TTS Model", choices=["ElevenLabs", "Parler TTS"], value="Parler TTS")
|
742 |
-
|
743 |
-
# gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
|
744 |
-
# chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
|
745 |
-
# chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
746 |
-
# bot_msg = chat_msg.then(bot, [chatbot, choice, tts_choice], [chatbot, gr.Audio(interactive=False, autoplay=True)])
|
747 |
-
# bot_msg.then(lambda: gr.Textbox(value="", interactive=True, placeholder="Ask Radar!!!...", show_label=False), None, [chat_input])
|
748 |
-
# chatbot.like(print_like_dislike, None, None)
|
749 |
-
# clear_button = gr.Button("Clear")
|
750 |
-
# clear_button.click(fn=clear_textbox, inputs=None, outputs=chat_input)
|
751 |
-
|
752 |
-
|
753 |
-
# audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy')
|
754 |
-
# audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="SAMLOne_real_time")
|
755 |
-
|
756 |
-
# # gr.Markdown("<h1 style='color: red;'>Map</h1>", elem_id="location-markdown")
|
757 |
-
# # location_output = gr.HTML()
|
758 |
-
# # bot_msg.then(show_map_if_details, [chatbot, choice], [location_output, location_output])
|
759 |
-
|
760 |
-
# # with gr.Column():
|
761 |
-
# # weather_output = gr.HTML(value=fetch_local_weather())
|
762 |
-
# # news_output = gr.HTML(value=fetch_local_news())
|
763 |
-
# # news_output = gr.HTML(value=fetch_local_events())
|
764 |
-
|
765 |
-
# with gr.Column():
|
766 |
-
|
767 |
-
# image_output_1 = gr.Image(value=generate_image(hardcoded_prompt_1), width=400, height=400)
|
768 |
-
# image_output_2 = gr.Image(value=generate_image(hardcoded_prompt_2), width=400, height=400)
|
769 |
-
# image_output_3 = gr.Image(value=generate_image(hardcoded_prompt_3), width=400, height=400)
|
770 |
-
|
771 |
-
|
772 |
-
# refresh_button = gr.Button("Refresh Images")
|
773 |
-
# refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
|
774 |
-
|
775 |
-
# demo.queue()
|
776 |
-
# demo.launch(share=True)
|
777 |
-
|
778 |
-
def generate_follow_up_buttons(response):
|
779 |
-
return gr.update(visible=True), gr.update(value=response)
|
780 |
-
|
781 |
-
def handle_follow_up_choice(choice, history):
|
782 |
-
follow_up_responses = {
|
783 |
-
"Question 1": "This is the response to follow-up question 1.",
|
784 |
-
"Question 2": "This is the response to follow-up question 2."
|
785 |
-
}
|
786 |
-
follow_up_response = follow_up_responses.get(choice, "Sorry, I didn't understand that choice.")
|
787 |
-
history.append((choice, follow_up_response))
|
788 |
-
return history, gr.update(visible=False)
|
789 |
-
|
790 |
-
|
791 |
-
|
792 |
with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
793 |
|
794 |
with gr.Row():
|
|
|
1 |
+
# import subprocess
|
2 |
+
# import sys
|
3 |
+
|
4 |
+
# def install_parler_tts():
|
5 |
+
# subprocess.check_call([sys.executable, "-m", "pip", "install", "git+https://github.com/huggingface/parler-tts.git"])
|
6 |
+
|
7 |
+
# # Call the function to install parler-tts
|
8 |
+
# install_parler_tts()
|
9 |
+
|
10 |
+
|
11 |
+
# import gradio as gr
|
12 |
+
# import requests
|
13 |
+
# import os
|
14 |
+
# import time
|
15 |
+
# import re
|
16 |
+
# import logging
|
17 |
+
# import tempfile
|
18 |
+
# import folium
|
19 |
+
# import concurrent.futures
|
20 |
+
# import torch
|
21 |
+
# from PIL import Image
|
22 |
+
# from datetime import datetime
|
23 |
+
# from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
24 |
+
# from googlemaps import Client as GoogleMapsClient
|
25 |
+
# from gtts import gTTS
|
26 |
+
# from diffusers import StableDiffusion3Pipeline
|
27 |
+
# import soundfile as sf
|
28 |
+
|
29 |
+
# from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
30 |
+
# from langchain_pinecone import PineconeVectorStore
|
31 |
+
# from langchain.prompts import PromptTemplate
|
32 |
+
# from langchain.chains import RetrievalQA
|
33 |
+
# from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
34 |
+
# from langchain.agents import Tool, initialize_agent
|
35 |
+
# from huggingface_hub import login
|
36 |
+
|
37 |
+
# # Check if the token is already set in the environment variables
|
38 |
+
# hf_token = os.getenv("HF_TOKEN")
|
39 |
+
|
40 |
+
# if hf_token is None:
|
41 |
+
# # If the token is not set, prompt for it (this should be done securely)
|
42 |
+
# print("Please set your Hugging Face token in the environment variables.")
|
43 |
+
# else:
|
44 |
+
# # Login using the token
|
45 |
+
# login(token=hf_token)
|
46 |
+
|
47 |
+
# # Your application logic goes here
|
48 |
+
# print("Logged in successfully to Hugging Face Hub!")
|
49 |
+
|
50 |
+
# # Set up logging
|
51 |
+
# logging.basicConfig(level=logging.DEBUG)
|
52 |
+
|
53 |
+
# # Initialize OpenAI embeddings
|
54 |
+
# embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
|
55 |
+
|
56 |
+
# # Initialize Pinecone
|
57 |
+
# from pinecone import Pinecone
|
58 |
+
# pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
|
59 |
+
|
60 |
+
# index_name = "birmingham-dataset"
|
61 |
+
# vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)
|
62 |
+
# retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
|
63 |
+
|
64 |
+
# # Initialize ChatOpenAI model
|
65 |
+
# chat_model = ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'],
|
66 |
+
# temperature=0, model='gpt-4o')
|
67 |
+
|
68 |
+
# conversational_memory = ConversationBufferWindowMemory(
|
69 |
+
# memory_key='chat_history',
|
70 |
+
# k=10,
|
71 |
+
# return_messages=True
|
72 |
+
# )
|
73 |
+
|
74 |
+
# def get_current_time_and_date():
|
75 |
+
# now = datetime.now()
|
76 |
+
# return now.strftime("%Y-%m-%d %H:%M:%S")
|
77 |
+
|
78 |
+
# # Example usage
|
79 |
+
# current_time_and_date = get_current_time_and_date()
|
80 |
+
|
81 |
+
# def fetch_local_events():
|
82 |
+
# api_key = os.environ['SERP_API']
|
83 |
+
# url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Birmingham&hl=en&gl=us&api_key={api_key}'
|
84 |
+
|
85 |
+
# response = requests.get(url)
|
86 |
+
# if response.status_code == 200:
|
87 |
+
# events_results = response.json().get("events_results", [])
|
88 |
+
# events_html = """
|
89 |
+
# <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Events</h2>
|
90 |
+
# <style>
|
91 |
+
# .event-item {
|
92 |
+
# font-family: 'Verdana', sans-serif;
|
93 |
+
# color: #333;
|
94 |
+
# margin-bottom: 15px;
|
95 |
+
# padding: 10px;
|
96 |
+
# font-weight: bold;
|
97 |
+
# }
|
98 |
+
# .event-item a {
|
99 |
+
# color: #1E90FF;
|
100 |
+
# text-decoration: none;
|
101 |
+
# }
|
102 |
+
# .event-item a:hover {
|
103 |
+
# text-decoration: underline;
|
104 |
+
# }
|
105 |
+
# </style>
|
106 |
+
# """
|
107 |
+
# for index, event in enumerate(events_results):
|
108 |
+
# title = event.get("title", "No title")
|
109 |
+
# date = event.get("date", "No date")
|
110 |
+
# location = event.get("address", "No location")
|
111 |
+
# link = event.get("link", "#")
|
112 |
+
# events_html += f"""
|
113 |
+
# <div class="event-item">
|
114 |
+
# <a href='{link}' target='_blank'>{index + 1}. {title}</a>
|
115 |
+
# <p>Date: {date}<br>Location: {location}</p>
|
116 |
+
# </div>
|
117 |
+
# """
|
118 |
+
# return events_html
|
119 |
+
# else:
|
120 |
+
# return "<p>Failed to fetch local events</p>"
|
121 |
+
|
122 |
+
# def fetch_local_weather():
|
123 |
+
# try:
|
124 |
+
# api_key = os.environ['WEATHER_API']
|
125 |
+
# url = f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/birmingham?unitGroup=metric&include=events%2Calerts%2Chours%2Cdays%2Ccurrent&key={api_key}'
|
126 |
+
# response = requests.get(url)
|
127 |
+
# response.raise_for_status()
|
128 |
+
# jsonData = response.json()
|
129 |
+
|
130 |
+
# current_conditions = jsonData.get("currentConditions", {})
|
131 |
+
# temp_celsius = current_conditions.get("temp", "N/A")
|
132 |
+
|
133 |
+
# if temp_celsius != "N/A":
|
134 |
+
# temp_fahrenheit = int((temp_celsius * 9/5) + 32)
|
135 |
+
# else:
|
136 |
+
# temp_fahrenheit = "N/A"
|
137 |
+
|
138 |
+
# condition = current_conditions.get("conditions", "N/A")
|
139 |
+
# humidity = current_conditions.get("humidity", "N/A")
|
140 |
+
|
141 |
+
# weather_html = f"""
|
142 |
+
# <div class="weather-theme">
|
143 |
+
# <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Weather</h2>
|
144 |
+
# <div class="weather-content">
|
145 |
+
# <div class="weather-icon">
|
146 |
+
# <img src="https://www.weatherbit.io/static/img/icons/{get_weather_icon(condition)}.png" alt="{condition}" style="width: 100px; height: 100px;">
|
147 |
+
# </div>
|
148 |
+
# <div class="weather-details">
|
149 |
+
# <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Temperature: {temp_fahrenheit}°F</p>
|
150 |
+
# <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Condition: {condition}</p>
|
151 |
+
# <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Humidity: {humidity}%</p>
|
152 |
+
# </div>
|
153 |
+
# </div>
|
154 |
+
# </div>
|
155 |
+
# <style>
|
156 |
+
# .weather-theme {{
|
157 |
+
# animation: backgroundAnimation 10s infinite alternate;
|
158 |
+
# border-radius: 10px;
|
159 |
+
# padding: 10px;
|
160 |
+
# margin-bottom: 15px;
|
161 |
+
# background: linear-gradient(45deg, #ffcc33, #ff6666, #ffcc33, #ff6666);
|
162 |
+
# background-size: 400% 400%;
|
163 |
+
# box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
164 |
+
# transition: box-shadow 0.3s ease, background-color 0.3s ease;
|
165 |
+
# }}
|
166 |
+
# .weather-theme:hover {{
|
167 |
+
# box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
|
168 |
+
# background-position: 100% 100%;
|
169 |
+
# }}
|
170 |
+
# @keyframes backgroundAnimation {{
|
171 |
+
# 0% {{ background-position: 0% 50%; }}
|
172 |
+
# 100% {{ background-position: 100% 50%; }}
|
173 |
+
# }}
|
174 |
+
# .weather-content {{
|
175 |
+
# display: flex;
|
176 |
+
# align-items: center;
|
177 |
+
# }}
|
178 |
+
# .weather-icon {{
|
179 |
+
# flex: 1;
|
180 |
+
# }}
|
181 |
+
# .weather-details {{
|
182 |
+
# flex: 3;
|
183 |
+
# }}
|
184 |
+
# </style>
|
185 |
+
# """
|
186 |
+
# return weather_html
|
187 |
+
# except requests.exceptions.RequestException as e:
|
188 |
+
# return f"<p>Failed to fetch local weather: {e}</p>"
|
189 |
+
|
190 |
+
# def get_weather_icon(condition):
|
191 |
+
# condition_map = {
|
192 |
+
# "Clear": "c01d",
|
193 |
+
# "Partly Cloudy": "c02d",
|
194 |
+
# "Cloudy": "c03d",
|
195 |
+
# "Overcast": "c04d",
|
196 |
+
# "Mist": "a01d",
|
197 |
+
# "Patchy rain possible": "r01d",
|
198 |
+
# "Light rain": "r02d",
|
199 |
+
# "Moderate rain": "r03d",
|
200 |
+
# "Heavy rain": "r04d",
|
201 |
+
# "Snow": "s01d",
|
202 |
+
# "Thunderstorm": "t01d",
|
203 |
+
# "Fog": "a05d",
|
204 |
+
# }
|
205 |
+
# return condition_map.get(condition, "c04d")
|
206 |
+
|
207 |
+
# # Update prompt templates to include fetched details
|
208 |
+
|
209 |
+
# current_time_and_date = get_current_time_and_date()
|
210 |
+
|
211 |
+
# # Define prompt templates
|
212 |
+
# template1 = """You are an expert concierge who is helpful and a renowned guide for Birmingham,Alabama. Based on weather being a sunny bright day and the today's date is 1st july 2024, use the following pieces of context,
|
213 |
+
# memory, and message history, along with your knowledge of perennial events in Birmingham,Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
|
214 |
+
# Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and
|
215 |
+
# event type and description. Always say "It was my pleasure!" at the end of the answer.
|
216 |
+
# {context}
|
217 |
+
# Question: {question}
|
218 |
+
# Helpful Answer:"""
|
219 |
+
|
220 |
+
# template2 = """You are an expert concierge who is helpful and a renowned guide for Birmingham,Alabama. Based on today's weather being a sunny bright day and today's date is 1st july 2024, take the location or address but don't show the location or address on the output prompts. Use the following pieces of context,
|
221 |
+
# memory, and message history, along with your knowledge of perennial events in Birmingham,Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
|
222 |
+
# Keep the answer short and sweet and crisp. Always say "It was my pleasure!" at the end of the answer.
|
223 |
+
# {context}
|
224 |
+
# Question: {question}
|
225 |
+
# Helpful Answer:"""
|
226 |
+
|
227 |
+
# QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
|
228 |
+
# QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
|
229 |
+
|
230 |
+
# # Define the retrieval QA chain
|
231 |
+
# def build_qa_chain(prompt_template):
|
232 |
+
# qa_chain = RetrievalQA.from_chain_type(
|
233 |
+
# llm=chat_model,
|
234 |
+
# chain_type="stuff",
|
235 |
+
# retriever=retriever,
|
236 |
+
# chain_type_kwargs={"prompt": prompt_template}
|
237 |
+
# )
|
238 |
+
# tools = [
|
239 |
+
# Tool(
|
240 |
+
# name='Knowledge Base',
|
241 |
+
# func=qa_chain,
|
242 |
+
# description='Use this tool when answering general knowledge queries to get more information about the topic'
|
243 |
+
# )
|
244 |
+
# ]
|
245 |
+
# return qa_chain, tools
|
246 |
+
|
247 |
+
# # Define the agent initializer
|
248 |
+
# def initialize_agent_with_prompt(prompt_template):
|
249 |
+
# qa_chain, tools = build_qa_chain(prompt_template)
|
250 |
+
# agent = initialize_agent(
|
251 |
+
# agent='chat-conversational-react-description',
|
252 |
+
# tools=tools,
|
253 |
+
# llm=chat_model,
|
254 |
+
# verbose=False,
|
255 |
+
# max_iteration=5,
|
256 |
+
# early_stopping_method='generate',
|
257 |
+
# memory=conversational_memory
|
258 |
+
# )
|
259 |
+
# return agent
|
260 |
+
|
261 |
+
# # Define the function to generate answers
|
262 |
+
# def generate_answer(message, choice):
|
263 |
+
# logging.debug(f"generate_answer called with prompt_choice: {choice}")
|
264 |
+
|
265 |
+
# if choice == "Details":
|
266 |
+
# agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_1)
|
267 |
+
# elif choice == "Conversational":
|
268 |
+
# agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
|
269 |
+
# else:
|
270 |
+
# logging.error(f"Invalid prompt_choice: {choice}. Defaulting to 'Conversational'")
|
271 |
+
# agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
|
272 |
+
# response = agent(message)
|
273 |
+
|
274 |
+
# # Extract addresses for mapping regardless of the choice
|
275 |
+
# addresses = extract_addresses(response['output'])
|
276 |
+
# return response['output'], addresses
|
277 |
+
|
278 |
+
# # def bot(history, choice, tts_model):
|
279 |
+
# # if not history:
|
280 |
+
# # return history
|
281 |
+
# # response, addresses = generate_answer(history[-1][0], choice)
|
282 |
+
# # history[-1][1] = ""
|
283 |
+
|
284 |
+
# # # Generate audio for the entire response in a separate thread
|
285 |
+
# # with concurrent.futures.ThreadPoolExecutor() as executor:
|
286 |
+
# # if tts_model == "ElevenLabs":
|
287 |
+
# # audio_future = executor.submit(generate_audio_elevenlabs, response)
|
288 |
+
# # else:
|
289 |
+
# # audio_future = executor.submit(generate_audio_parler_tts, response)
|
290 |
+
|
291 |
+
# # for character in response:
|
292 |
+
# # history[-1][1] += character
|
293 |
+
# # time.sleep(0.05) # Adjust the speed of text appearance
|
294 |
+
# # yield history, None
|
295 |
+
|
296 |
+
# # audio_path = audio_future.result()
|
297 |
+
# # yield history, audio_path
|
298 |
+
|
299 |
+
# def bot(history, choice, tts_model):
|
300 |
+
# if not history:
|
301 |
+
# return history
|
302 |
+
# response, addresses = generate_answer(history[-1][0], choice)
|
303 |
+
# history[-1][1] = ""
|
304 |
+
|
305 |
+
# with concurrent.futures.ThreadPoolExecutor() as executor:
|
306 |
+
# if tts_model == "ElevenLabs":
|
307 |
+
# audio_future = executor.submit(generate_audio_elevenlabs, response)
|
308 |
+
# else:
|
309 |
+
# audio_future = executor.submit(generate_audio_parler_tts, response)
|
310 |
+
|
311 |
+
# for character in response:
|
312 |
+
# history[-1][1] += character
|
313 |
+
# time.sleep(0.05)
|
314 |
+
# yield history, None, gr.update(visible=True, value=history[-1][1])
|
315 |
+
|
316 |
+
# audio_path = audio_future.result()
|
317 |
+
# yield history, audio_path, gr.update(visible=True, value=history[-1][1])
|
318 |
+
|
319 |
+
# def add_message(history, message):
|
320 |
+
# history.append((message, None))
|
321 |
+
# return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
|
322 |
+
|
323 |
+
# def print_like_dislike(x: gr.LikeData):
|
324 |
+
# print(x.index, x.value, x.liked)
|
325 |
+
|
326 |
+
# def extract_addresses(response):
|
327 |
+
# if not isinstance(response, str):
|
328 |
+
# response = str(response)
|
329 |
+
# address_patterns = [
|
330 |
+
# r'([A-Z].*,\sBirmingham,\sAL\s\d{5})',
|
331 |
+
# r'(\d{4}\s.*,\sBirmingham,\sAL\s\d{5})',
|
332 |
+
# r'([A-Z].*,\sAL\s\d{5})',
|
333 |
+
# r'([A-Z].*,.*\sSt,\sBirmingham,\sAL\s\d{5})',
|
334 |
+
# r'([A-Z].*,.*\sStreets,\sBirmingham,\sAL\s\d{5})',
|
335 |
+
# r'(\d{2}.*\sStreets)',
|
336 |
+
# r'([A-Z].*\s\d{2},\sBirmingham,\sAL\s\d{5})'
|
337 |
+
# r'([a-zA-Z]\s Birmingham)'
|
338 |
+
# ]
|
339 |
+
# addresses = []
|
340 |
+
# for pattern in address_patterns:
|
341 |
+
# addresses.extend(re.findall(pattern, response))
|
342 |
+
# return addresses
|
343 |
+
|
344 |
+
# all_addresses = []
|
345 |
+
|
346 |
+
# def generate_map(location_names):
|
347 |
+
# global all_addresses
|
348 |
+
# all_addresses.extend(location_names)
|
349 |
+
|
350 |
+
# api_key = os.environ['GOOGLEMAPS_API_KEY']
|
351 |
+
# gmaps = GoogleMapsClient(key=api_key)
|
352 |
+
|
353 |
+
# m = folium.Map(location=[33.5175,-86.809444], zoom_start=16)
|
354 |
+
|
355 |
+
# for location_name in all_addresses:
|
356 |
+
# geocode_result = gmaps.geocode(location_name)
|
357 |
+
# if geocode_result:
|
358 |
+
# location = geocode_result[0]['geometry']['location']
|
359 |
+
# folium.Marker(
|
360 |
+
# [location['lat'], location['lng']],
|
361 |
+
# tooltip=f"{geocode_result[0]['formatted_address']}"
|
362 |
+
# ).add_to(m)
|
363 |
+
|
364 |
+
# map_html = m._repr_html_()
|
365 |
+
# return map_html
|
366 |
+
|
367 |
+
# def fetch_local_news():
|
368 |
+
# api_key = os.environ['SERP_API']
|
369 |
+
# url = f'https://serpapi.com/search.json?engine=google_news&q=birmingham headline&api_key={api_key}'
|
370 |
+
# response = requests.get(url)
|
371 |
+
# if response.status_code == 200:
|
372 |
+
# results = response.json().get("news_results", [])
|
373 |
+
# news_html = """
|
374 |
+
# <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Birmingham Today</h2>
|
375 |
+
# <style>
|
376 |
+
# .news-item {
|
377 |
+
# font-family: 'Verdana', sans-serif;
|
378 |
+
# color: #333;
|
379 |
+
# background-color: #f0f8ff;
|
380 |
+
# margin-bottom: 15px;
|
381 |
+
# padding: 10px;
|
382 |
+
# border-radius: 5px;
|
383 |
+
# transition: box-shadow 0.3s ease, background-color 0.3s ease;
|
384 |
+
# font-weight: bold;
|
385 |
+
# }
|
386 |
+
# .news-item:hover {
|
387 |
+
# box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
388 |
+
# background-color: #e6f7ff;
|
389 |
+
# }
|
390 |
+
# .news-item a {
|
391 |
+
# color: #1E90FF;
|
392 |
+
# text-decoration: none;
|
393 |
+
# font-weight: bold;
|
394 |
+
# }
|
395 |
+
# .news-item a:hover {
|
396 |
+
# text-decoration: underline;
|
397 |
+
# }
|
398 |
+
# .news-preview {
|
399 |
+
# position: absolute;
|
400 |
+
# display: none;
|
401 |
+
# border: 1px solid #ccc;
|
402 |
+
# border-radius: 5px;
|
403 |
+
# box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
|
404 |
+
# background-color: white;
|
405 |
+
# z-index: 1000;
|
406 |
+
# max-width: 300px;
|
407 |
+
# padding: 10px;
|
408 |
+
# font-family: 'Verdana', sans-serif;
|
409 |
+
# color: #333;
|
410 |
+
# }
|
411 |
+
# </style>
|
412 |
+
# <script>
|
413 |
+
# function showPreview(event, previewContent) {
|
414 |
+
# var previewBox = document.getElementById('news-preview');
|
415 |
+
# previewBox.innerHTML = previewContent;
|
416 |
+
# previewBox.style.left = event.pageX + 'px';
|
417 |
+
# previewBox.style.top = event.pageY + 'px';
|
418 |
+
# previewBox.style.display = 'block';
|
419 |
+
# }
|
420 |
+
# function hidePreview() {
|
421 |
+
# var previewBox = document.getElementById('news-preview');
|
422 |
+
# previewBox.style.display = 'none';
|
423 |
+
# }
|
424 |
+
# </script>
|
425 |
+
# <div id="news-preview" class="news-preview"></div>
|
426 |
+
# """
|
427 |
+
# for index, result in enumerate(results[:7]):
|
428 |
+
# title = result.get("title", "No title")
|
429 |
+
# link = result.get("link", "#")
|
430 |
+
# snippet = result.get("snippet", "")
|
431 |
+
# news_html += f"""
|
432 |
+
# <div class="news-item" onmouseover="showPreview(event, '{snippet}')" onmouseout="hidePreview()">
|
433 |
+
# <a href='{link}' target='_blank'>{index + 1}. {title}</a>
|
434 |
+
# <p>{snippet}</p>
|
435 |
+
# </div>
|
436 |
+
# """
|
437 |
+
# return news_html
|
438 |
+
# else:
|
439 |
+
# return "<p>Failed to fetch local news</p>"
|
440 |
+
|
441 |
+
# # Voice Control
|
442 |
+
# import numpy as np
|
443 |
+
# import torch
|
444 |
+
# from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
445 |
+
# from parler_tts import ParlerTTSForConditionalGeneration
|
446 |
+
# from transformers import AutoTokenizer
|
447 |
+
|
448 |
+
# model_id = 'openai/whisper-large-v3'
|
449 |
+
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
450 |
+
# torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
451 |
+
# model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype,
|
452 |
+
# #low_cpu_mem_usage=True,
|
453 |
+
# use_safetensors=True).to(device)
|
454 |
+
# processor = AutoProcessor.from_pretrained(model_id)
|
455 |
+
|
456 |
+
# # Optimized ASR pipeline
|
457 |
+
# pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=True)
|
458 |
+
|
459 |
+
# base_audio_drive = "/data/audio"
|
460 |
+
|
461 |
+
# import numpy as np
|
462 |
+
|
463 |
+
# def transcribe_function(stream, new_chunk):
|
464 |
+
# try:
|
465 |
+
# sr, y = new_chunk[0], new_chunk[1]
|
466 |
+
# except TypeError:
|
467 |
+
# print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
|
468 |
+
# return stream, "", None
|
469 |
+
|
470 |
+
# y = y.astype(np.float32) / np.max(np.abs(y))
|
471 |
+
|
472 |
+
# if stream is not None:
|
473 |
+
# stream = np.concatenate([stream, y])
|
474 |
+
# else:
|
475 |
+
# stream = y
|
476 |
+
|
477 |
+
# result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
|
478 |
+
|
479 |
+
# full_text = result.get("text", "")
|
480 |
+
|
481 |
+
# return stream, full_text, result
|
482 |
+
|
483 |
+
# def update_map_with_response(history):
|
484 |
+
# if not history:
|
485 |
+
# return ""
|
486 |
+
# response = history[-1][1]
|
487 |
+
# addresses = extract_addresses(response)
|
488 |
+
# return generate_map(addresses)
|
489 |
+
|
490 |
+
# def clear_textbox():
|
491 |
+
# return ""
|
492 |
+
|
493 |
+
# def show_map_if_details(history,choice):
|
494 |
+
# if choice in ["Details", "Conversational"]:
|
495 |
+
# return gr.update(visible=True), update_map_with_response(history)
|
496 |
+
# else:
|
497 |
+
# return gr.update(visible=False), ""
|
498 |
+
|
499 |
+
# def generate_audio_elevenlabs(text):
|
500 |
+
# XI_API_KEY = os.environ['ELEVENLABS_API']
|
501 |
+
# VOICE_ID = 'd9MIrwLnvDeH7aZb61E9' # Replace with your voice ID
|
502 |
+
# tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
|
503 |
+
# headers = {
|
504 |
+
# "Accept": "application/json",
|
505 |
+
# "xi-api-key": XI_API_KEY
|
506 |
+
# }
|
507 |
+
# data = {
|
508 |
+
# "text": str(text),
|
509 |
+
# "model_id": "eleven_multilingual_v2",
|
510 |
+
# "voice_settings": {
|
511 |
+
# "stability": 1.0,
|
512 |
+
# "similarity_boost": 0.0,
|
513 |
+
# "style": 0.60, # Adjust style for more romantic tone
|
514 |
+
# "use_speaker_boost": False
|
515 |
+
# }
|
516 |
+
# }
|
517 |
+
# response = requests.post(tts_url, headers=headers, json=data, stream=True)
|
518 |
+
# if response.ok:
|
519 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
|
520 |
+
# for chunk in response.iter_content(chunk_size=1024):
|
521 |
+
# f.write(chunk)
|
522 |
+
# temp_audio_path = f.name
|
523 |
+
# logging.debug(f"Audio saved to {temp_audio_path}")
|
524 |
+
# return temp_audio_path
|
525 |
+
# else:
|
526 |
+
# logging.error(f"Error generating audio: {response.text}")
|
527 |
+
# return None
|
528 |
+
|
529 |
+
# # def generate_audio_parler_tts(text):
|
530 |
+
# # model_id = 'parler-tts/parler_tts_mini_v0.1'
|
531 |
+
# # device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
532 |
+
# # try:
|
533 |
+
# # model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
534 |
+
# # except torch.cuda.OutOfMemoryError:
|
535 |
+
# # print("CUDA out of memory. Switching to CPU.")
|
536 |
+
# # device = "cpu"
|
537 |
+
# # model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
538 |
+
# # tokenizer = AutoTokenizer.from_pretrained(model_id)
|
539 |
+
|
540 |
+
# # description = "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast."
|
541 |
+
|
542 |
+
# # input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
543 |
+
# # prompt_input_ids = tokenizer(text, return_tensors="pt").input_ids.to(device)
|
544 |
+
|
545 |
+
# # generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
546 |
+
# # audio_arr = generation.cpu().numpy().squeeze()
|
547 |
+
|
548 |
+
# # with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
549 |
+
# # sf.write(f.name, audio_arr, model.config.sampling_rate)
|
550 |
+
# # temp_audio_path = f.name
|
551 |
+
|
552 |
+
# # logging.debug(f"Audio saved to {temp_audio_path}")
|
553 |
+
# # return temp_audio_path
|
554 |
+
|
555 |
+
# # def generate_audio_parler_tts(text):
|
556 |
+
# # model_id = 'parler-tts/parler_tts_mini_v0.1'
|
557 |
+
# # device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
558 |
+
# # try:
|
559 |
+
# # model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
560 |
+
# # except torch.cuda.OutOfMemoryError:
|
561 |
+
# # print("CUDA out of memory. Switching to CPU.")
|
562 |
+
# # device = "cpu"
|
563 |
+
# # model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
564 |
+
# # tokenizer = AutoTokenizer.from_pretrained(model_id)
|
565 |
+
|
566 |
+
# # description = "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast."
|
567 |
+
|
568 |
+
# # input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
569 |
+
# # max_length = model.config.max_length
|
570 |
+
|
571 |
+
# # # Split the text into smaller chunks if it exceeds the max length
|
572 |
+
# # text_chunks = [text[i:i+max_length] for i in range(0, len(text), max_length)]
|
573 |
+
# # audio_segments = []
|
574 |
+
|
575 |
+
# # for chunk in text_chunks:
|
576 |
+
# # prompt_input_ids = tokenizer(chunk, return_tensors="pt").input_ids.to(device)
|
577 |
+
# # generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
578 |
+
# # audio_arr = generation.cpu().numpy().squeeze()
|
579 |
+
# # audio_segments.append(audio_arr)
|
580 |
+
|
581 |
+
# # combined_audio = np.concatenate(audio_segments)
|
582 |
+
|
583 |
+
# # with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
584 |
+
# # sf.write(f.name, combined_audio, model.config.sampling_rate)
|
585 |
+
# # temp_audio_path = f.name
|
586 |
+
|
587 |
+
# # logging.debug(f"Audio saved to {temp_audio_path}")
|
588 |
+
# # return temp_audio_path
|
589 |
+
|
590 |
+
# # def generate_audio_parler_tts(text, chunk_size=200):
|
591 |
+
# # def split_text(text, chunk_size):
|
592 |
+
# # # Split text into chunks of the specified size
|
593 |
+
# # words = text.split()
|
594 |
+
# # chunks = []
|
595 |
+
# # current_chunk = []
|
596 |
+
# # current_length = 0
|
597 |
+
|
598 |
+
# # for word in words:
|
599 |
+
# # if current_length + len(word) + 1 > chunk_size:
|
600 |
+
# # chunks.append(" ".join(current_chunk))
|
601 |
+
# # current_chunk = [word]
|
602 |
+
# # current_length = len(word) + 1
|
603 |
+
# # else:
|
604 |
+
# # current_chunk.append(word)
|
605 |
+
# # current_length += len(word) + 1
|
606 |
+
|
607 |
+
# # if current_chunk:
|
608 |
+
# # chunks.append(" ".join(current_chunk))
|
609 |
+
|
610 |
+
# # return chunks
|
611 |
+
|
612 |
+
# # model_id = 'parler-tts/parler_tts_mini_v0.1'
|
613 |
+
# # device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
614 |
+
# # try:
|
615 |
+
# # model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
616 |
+
# # except torch.cuda.OutOfMemoryError:
|
617 |
+
# # print("CUDA out of memory. Switching to CPU.")
|
618 |
+
# # device = "cpu"
|
619 |
+
# # model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
620 |
+
# # tokenizer = AutoTokenizer.from_pretrained(model_id)
|
621 |
+
|
622 |
+
# # description = "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast."
|
623 |
+
|
624 |
+
# # input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
625 |
+
# # chunks = split_text(text, chunk_size)
|
626 |
+
# # audio_arrs = []
|
627 |
+
|
628 |
+
# # for chunk in chunks:
|
629 |
+
# # prompt_input_ids = tokenizer(chunk, return_tensors="pt").input_ids.to(device)
|
630 |
+
# # generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
631 |
+
# # audio_arr = generation.cpu().numpy().squeeze()
|
632 |
+
# # audio_arrs.append(audio_arr)
|
633 |
+
|
634 |
+
# # # Concatenate all audio arrays into a single array
|
635 |
+
# # concatenated_audio = np.concatenate(audio_arrs)
|
636 |
+
|
637 |
+
# # with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
638 |
+
# # sf.write(f.name, concatenated_audio, model.config.sampling_rate)
|
639 |
+
# # temp_audio_path = f.name
|
640 |
+
|
641 |
+
# # logging.debug(f"Audio saved to {temp_audio_path}")
|
642 |
+
# # return temp_audio_path
|
643 |
+
|
644 |
+
|
645 |
+
# import concurrent.futures
|
646 |
+
|
647 |
+
# def generate_audio_parler_tts(text, chunk_size=200):
|
648 |
+
# def split_text(text, chunk_size):
|
649 |
+
# words = text.split()
|
650 |
+
# chunks = []
|
651 |
+
# current_chunk = []
|
652 |
+
# current_length = 0
|
653 |
+
|
654 |
+
# for word in words:
|
655 |
+
# if current_length + len(word) + 1 > chunk_size:
|
656 |
+
# chunks.append(" ".join(current_chunk))
|
657 |
+
# current_chunk = [word]
|
658 |
+
# current_length = len(word) + 1
|
659 |
+
# else:
|
660 |
+
# current_chunk.append(word)
|
661 |
+
# current_length += len(word) + 1
|
662 |
+
|
663 |
+
# if current_chunk:
|
664 |
+
# chunks.append(" ".join(current_chunk))
|
665 |
+
|
666 |
+
# return chunks
|
667 |
+
|
668 |
+
# def process_chunk(chunk):
|
669 |
+
# prompt_input_ids = tokenizer(chunk, return_tensors="pt").input_ids.to(device)
|
670 |
+
# generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
671 |
+
# audio_arr = generation.cpu().numpy().squeeze()
|
672 |
+
# return audio_arr
|
673 |
+
|
674 |
+
# model_id = 'parler-tts/parler_tts_mini_v0.1'
|
675 |
+
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
676 |
+
# try:
|
677 |
+
# model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
678 |
+
# except torch.cuda.OutOfMemoryError:
|
679 |
+
# print("CUDA out of memory. Switching to CPU.")
|
680 |
+
# device = "cpu"
|
681 |
+
# model = ParlerTTSForConditionalGeneration.from_pretrained(model_id).to(device)
|
682 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_id)
|
683 |
+
|
684 |
+
# description = "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast."
|
685 |
+
|
686 |
+
# input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
687 |
+
# chunks = split_text(text, chunk_size)
|
688 |
+
|
689 |
+
# # Process chunks in parallel
|
690 |
+
# with concurrent.futures.ThreadPoolExecutor() as executor:
|
691 |
+
# futures = [executor.submit(process_chunk, chunk) for chunk in chunks]
|
692 |
+
# audio_arrs = [future.result() for future in concurrent.futures.as_completed(futures)]
|
693 |
+
|
694 |
+
# # Concatenate all audio arrays into a single array
|
695 |
+
# concatenated_audio = np.concatenate(audio_arrs)
|
696 |
+
|
697 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
698 |
+
# sf.write(f.name, concatenated_audio, model.config.sampling_rate)
|
699 |
+
# temp_audio_path = f.name
|
700 |
+
|
701 |
+
# logging.debug(f"Audio saved to {temp_audio_path}")
|
702 |
+
# return temp_audio_path
|
703 |
+
|
704 |
+
|
705 |
+
|
706 |
+
|
707 |
+
|
708 |
+
|
709 |
+
# # Stable Diffusion setup
|
710 |
+
# pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
|
711 |
+
# pipe = pipe.to("cuda")
|
712 |
+
|
713 |
+
# def generate_image(prompt):
|
714 |
+
# image = pipe(
|
715 |
+
# prompt,
|
716 |
+
# negative_prompt="",
|
717 |
+
# num_inference_steps=28,
|
718 |
+
# guidance_scale=3.0,
|
719 |
+
# ).images[0]
|
720 |
+
# return image
|
721 |
+
|
722 |
+
# # Hardcoded prompt for image generation
|
723 |
+
# hardcoded_prompt_1="Give a high quality photograph of a great looking red 2026 Bentley coupe against a skyline setting in th night, michael mann style in omaha enticing the consumer to buy this product"
|
724 |
+
# hardcoded_prompt_2="A vibrant and dynamic football game scene in the style of Peter Paul Rubens, showcasing the intense match between Alabama and Nebraska. The players are depicted with the dramatic, muscular physiques and expressive faces typical of Rubens' style. The Alabama team is wearing their iconic crimson and white uniforms, while the Nebraska team is in their classic red and white attire. The scene is filled with action, with players in mid-motion, tackling, running, and catching the ball. The background features a grand stadium filled with cheering fans, banners, and the natural landscape in the distance. The colors are rich and vibrant, with a strong use of light and shadow to create depth and drama. The overall atmosphere captures the intensity and excitement of the game, infused with the grandeur and dynamism characteristic of Rubens' work."
|
725 |
+
# hardcoded_prompt_3 = "Create a high-energy scene of a DJ performing on a large stage with vibrant lights, colorful lasers, a lively dancing crowd, and various electronic equipment in the background."
|
726 |
+
|
727 |
+
# def update_images():
|
728 |
+
# image_1 = generate_image(hardcoded_prompt_1)
|
729 |
+
# image_2 = generate_image(hardcoded_prompt_2)
|
730 |
+
# image_3 = generate_image(hardcoded_prompt_3)
|
731 |
+
# return image_1, image_2, image_3
|
732 |
+
|
733 |
+
# # with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
734 |
+
|
735 |
+
# # with gr.Row():
|
736 |
+
# # with gr.Column():
|
737 |
+
# # state = gr.State()
|
738 |
+
|
739 |
+
# # chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
|
740 |
+
# # choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
|
741 |
+
# # tts_choice = gr.Radio(label="Select TTS Model", choices=["ElevenLabs", "Parler TTS"], value="Parler TTS")
|
742 |
+
|
743 |
+
# # gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
|
744 |
+
# # chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
|
745 |
+
# # chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
746 |
+
# # bot_msg = chat_msg.then(bot, [chatbot, choice, tts_choice], [chatbot, gr.Audio(interactive=False, autoplay=True)])
|
747 |
+
# # bot_msg.then(lambda: gr.Textbox(value="", interactive=True, placeholder="Ask Radar!!!...", show_label=False), None, [chat_input])
|
748 |
+
# # chatbot.like(print_like_dislike, None, None)
|
749 |
+
# # clear_button = gr.Button("Clear")
|
750 |
+
# # clear_button.click(fn=clear_textbox, inputs=None, outputs=chat_input)
|
751 |
+
|
752 |
+
|
753 |
+
# # audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy')
|
754 |
+
# # audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="SAMLOne_real_time")
|
755 |
+
|
756 |
+
# # # gr.Markdown("<h1 style='color: red;'>Map</h1>", elem_id="location-markdown")
|
757 |
+
# # # location_output = gr.HTML()
|
758 |
+
# # # bot_msg.then(show_map_if_details, [chatbot, choice], [location_output, location_output])
|
759 |
+
|
760 |
+
# # # with gr.Column():
|
761 |
+
# # # weather_output = gr.HTML(value=fetch_local_weather())
|
762 |
+
# # # news_output = gr.HTML(value=fetch_local_news())
|
763 |
+
# # # news_output = gr.HTML(value=fetch_local_events())
|
764 |
+
|
765 |
+
# # with gr.Column():
|
766 |
+
|
767 |
+
# # image_output_1 = gr.Image(value=generate_image(hardcoded_prompt_1), width=400, height=400)
|
768 |
+
# # image_output_2 = gr.Image(value=generate_image(hardcoded_prompt_2), width=400, height=400)
|
769 |
+
# # image_output_3 = gr.Image(value=generate_image(hardcoded_prompt_3), width=400, height=400)
|
770 |
+
|
771 |
+
|
772 |
+
# # refresh_button = gr.Button("Refresh Images")
|
773 |
+
# # refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
|
774 |
+
|
775 |
+
# # demo.queue()
|
776 |
+
# # demo.launch(share=True)
|
777 |
+
|
778 |
+
# def generate_follow_up_buttons(response):
|
779 |
+
# return gr.update(visible=True), gr.update(value=response)
|
780 |
+
|
781 |
+
# def handle_follow_up_choice(choice, history):
|
782 |
+
# follow_up_responses = {
|
783 |
+
# "Question 1": "This is the response to follow-up question 1.",
|
784 |
+
# "Question 2": "This is the response to follow-up question 2."
|
785 |
+
# }
|
786 |
+
# follow_up_response = follow_up_responses.get(choice, "Sorry, I didn't understand that choice.")
|
787 |
+
# history.append((choice, follow_up_response))
|
788 |
+
# return history, gr.update(visible=False)
|
789 |
+
|
790 |
+
|
791 |
+
|
792 |
+
# with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
793 |
+
|
794 |
+
# with gr.Row():
|
795 |
+
# with gr.Column():
|
796 |
+
# state = gr.State()
|
797 |
+
# chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
|
798 |
+
# choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
|
799 |
+
# tts_choice = gr.Radio(label="Select TTS Model", choices=["ElevenLabs", "Parler TTS"], value="Parler TTS")
|
800 |
+
|
801 |
+
# gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
|
802 |
+
# chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
|
803 |
+
# chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
804 |
+
# bot_msg = chat_msg.then(bot, [chatbot, choice, tts_choice], [chatbot, gr.Audio(interactive=False, autoplay=True), gr.Button("Follow-up Question 1", visible=False), gr.Button("Follow-up Question 2", visible=False)])
|
805 |
+
# bot_msg.then(lambda: gr.Textbox(value="", interactive=True, placeholder="Ask Radar!!!...", show_label=False), None, [chat_input])
|
806 |
+
|
807 |
+
# follow_up_button_1 = gr.Button("Follow-up Question 1", visible=False)
|
808 |
+
# follow_up_button_2 = gr.Button("Follow-up Question 2", visible=False)
|
809 |
+
# follow_up_button_1.click(handle_follow_up_choice, inputs=[follow_up_button_1, chatbot], outputs=[chatbot, follow_up_button_1, follow_up_button_2])
|
810 |
+
# follow_up_button_2.click(handle_follow_up_choice, inputs=[follow_up_button_2, chatbot], outputs=[chatbot, follow_up_button_1, follow_up_button_2])
|
811 |
+
|
812 |
+
# chatbot.like(print_like_dislike, None, None)
|
813 |
+
# clear_button = gr.Button("Clear")
|
814 |
+
# clear_button.click(fn=clear_textbox, inputs=None, outputs=chat_input)
|
815 |
+
|
816 |
+
# audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy')
|
817 |
+
# audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="SAMLOne_real_time")
|
818 |
+
|
819 |
+
# with gr.Column():
|
820 |
+
# image_output_1 = gr.Image(value=generate_image(hardcoded_prompt_1), width=400, height=400)
|
821 |
+
# image_output_2 = gr.Image(value=generate_image(hardcoded_prompt_2), width=400, height=400)
|
822 |
+
# image_output_3 = gr.Image(value=generate_image(hardcoded_prompt_3), width=400, height=400)
|
823 |
+
|
824 |
+
# refresh_button = gr.Button("Refresh Images")
|
825 |
+
# refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
|
826 |
+
|
827 |
+
# demo.queue()
|
828 |
+
# demo.launch(share=True)
|
829 |
+
|
830 |
+
|
831 |
+
|
832 |
+
|
833 |
+
|
834 |
+
|
835 |
|
|
|
|
|
836 |
|
|
|
|
|
837 |
|
838 |
|
839 |
import gradio as gr
|
|
|
851 |
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
852 |
from googlemaps import Client as GoogleMapsClient
|
853 |
from gtts import gTTS
|
854 |
+
from diffusers import StableDiffusionPipeline
|
855 |
import soundfile as sf
|
856 |
|
857 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
|
|
862 |
from langchain.agents import Tool, initialize_agent
|
863 |
from huggingface_hub import login
|
864 |
|
865 |
+
# Ensure you have your HF_TOKEN set
|
866 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
867 |
if hf_token is None:
|
|
|
868 |
print("Please set your Hugging Face token in the environment variables.")
|
869 |
else:
|
|
|
870 |
login(token=hf_token)
|
871 |
|
872 |
+
# Logging setup
|
|
|
|
|
|
|
873 |
logging.basicConfig(level=logging.DEBUG)
|
874 |
|
875 |
# Initialize OpenAI embeddings
|
|
|
884 |
retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
|
885 |
|
886 |
# Initialize ChatOpenAI model
|
887 |
+
chat_model = ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'], temperature=0, model='gpt-4')
|
|
|
888 |
|
889 |
+
conversational_memory = ConversationBufferWindowMemory(memory_key='chat_history', k=10, return_messages=True)
|
|
|
|
|
|
|
|
|
890 |
|
891 |
def get_current_time_and_date():
|
892 |
now = datetime.now()
|
893 |
return now.strftime("%Y-%m-%d %H:%M:%S")
|
894 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
895 |
# Define prompt templates
|
896 |
+
template1 = """You are an expert concierge who is helpful and a renowned guide for Birmingham, Alabama. Based on weather being a sunny bright day and today's date is 1st July 2024, use the following pieces of context, memory, and message history, along with your knowledge of perennial events in Birmingham, Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer. Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and event type and description. Always say "It was my pleasure!" at the end of the answer.
|
|
|
|
|
|
|
897 |
{context}
|
898 |
Question: {question}
|
899 |
Helpful Answer:"""
|
900 |
|
901 |
+
template2 = """You are an expert concierge who is helpful and a renowned guide for Birmingham, Alabama. Based on today's weather being a sunny bright day and today's date is 1st July 2024, take the location or address but don't show the location or address on the output prompts. Use the following pieces of context, memory, and message history, along with your knowledge of perennial events in Birmingham, Alabama, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer. Keep the answer short and sweet and crisp. Always say "It was my pleasure!" at the end of the answer.
|
|
|
|
|
902 |
{context}
|
903 |
Question: {question}
|
904 |
Helpful Answer:"""
|
|
|
914 |
retriever=retriever,
|
915 |
chain_type_kwargs={"prompt": prompt_template}
|
916 |
)
|
917 |
+
tools = [Tool(name='Knowledge Base', func=qa_chain, description='Use this tool when answering general knowledge queries to get more information about the topic')]
|
|
|
|
|
|
|
|
|
|
|
|
|
918 |
return qa_chain, tools
|
919 |
|
920 |
# Define the agent initializer
|
|
|
934 |
# Define the function to generate answers
|
935 |
def generate_answer(message, choice):
|
936 |
logging.debug(f"generate_answer called with prompt_choice: {choice}")
|
|
|
937 |
if choice == "Details":
|
938 |
agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_1)
|
939 |
elif choice == "Conversational":
|
|
|
942 |
logging.error(f"Invalid prompt_choice: {choice}. Defaulting to 'Conversational'")
|
943 |
agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
|
944 |
response = agent(message)
|
945 |
+
return response['output']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
946 |
|
947 |
def bot(history, choice, tts_model):
|
948 |
if not history:
|
949 |
return history
|
950 |
+
response = generate_answer(history[-1][0], choice)
|
951 |
history[-1][1] = ""
|
952 |
|
953 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
|
959 |
for character in response:
|
960 |
history[-1][1] += character
|
961 |
time.sleep(0.05)
|
962 |
+
yield history, None, gr.update(visible=True, value=history[-1][1]), gr.update(visible=True), gr.update(visible=True)
|
963 |
|
964 |
audio_path = audio_future.result()
|
965 |
+
yield history, audio_path, gr.update(visible=True, value=history[-1][1]), gr.update(visible=True), gr.update(visible=True)
|
966 |
+
|
967 |
+
def handle_follow_up_choice(choice, history):
|
968 |
+
follow_up_responses = {
|
969 |
+
"Follow-up Question 1": "This is the response to follow-up question 1.",
|
970 |
+
"Follow-up Question 2": "This is the response to follow-up question 2."
|
971 |
+
}
|
972 |
+
follow_up_response = follow_up_responses.get(choice, "Sorry, I didn't understand that choice.")
|
973 |
+
history.append((choice, follow_up_response))
|
974 |
+
return history, gr.update(visible=False), gr.update(visible=False)
|
975 |
|
976 |
def add_message(history, message):
|
977 |
history.append((message, None))
|
978 |
return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
|
979 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
980 |
def clear_textbox():
|
981 |
+
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
982 |
|
983 |
def generate_audio_parler_tts(text, chunk_size=200):
|
984 |
def split_text(text, chunk_size):
|
|
|
986 |
chunks = []
|
987 |
current_chunk = []
|
988 |
current_length = 0
|
989 |
+
|
990 |
for word in words:
|
991 |
if current_length + len(word) + 1 > chunk_size:
|
992 |
chunks.append(" ".join(current_chunk))
|
|
|
995 |
else:
|
996 |
current_chunk.append(word)
|
997 |
current_length += len(word) + 1
|
998 |
+
|
999 |
if current_chunk:
|
1000 |
chunks.append(" ".join(current_chunk))
|
1001 |
+
|
1002 |
return chunks
|
1003 |
|
1004 |
def process_chunk(chunk):
|
|
|
1021 |
|
1022 |
input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
1023 |
chunks = split_text(text, chunk_size)
|
1024 |
+
|
|
|
1025 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
1026 |
futures = [executor.submit(process_chunk, chunk) for chunk in chunks]
|
1027 |
audio_arrs = [future.result() for future in concurrent.futures.as_completed(futures)]
|
1028 |
+
|
|
|
1029 |
concatenated_audio = np.concatenate(audio_arrs)
|
1030 |
+
|
1031 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
1032 |
sf.write(f.name, concatenated_audio, model.config.sampling_rate)
|
1033 |
temp_audio_path = f.name
|
|
|
1035 |
logging.debug(f"Audio saved to {temp_audio_path}")
|
1036 |
return temp_audio_path
|
1037 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1038 |
with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
1039 |
|
1040 |
with gr.Row():
|