Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -43,7 +43,7 @@ from langchain_core.pydantic_v1 import BaseModel, Field
|
|
43 |
from langchain_core.messages import AIMessage, HumanMessage
|
44 |
from langchain_core.output_parsers import StrOutputParser
|
45 |
from langchain_core.runnables import RunnableBranch, RunnableLambda, RunnableParallel, RunnablePassthrough
|
46 |
-
|
47 |
|
48 |
# Set environment variables for CUDA
|
49 |
os.environ['PYTORCH_USE_CUDA_DSA'] = '1'
|
@@ -277,11 +277,48 @@ def generate_answer(message, choice, retrieval_mode):
|
|
277 |
else:
|
278 |
return "Invalid retrieval mode selected.", []
|
279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
def bot(history, choice, tts_choice, retrieval_mode):
|
281 |
if not history:
|
282 |
return history
|
283 |
|
284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
285 |
history[-1][1] = ""
|
286 |
|
287 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
@@ -302,6 +339,7 @@ def bot(history, choice, tts_choice, retrieval_mode):
|
|
302 |
|
303 |
history.append([response, None]) # Ensure the response is added in the correct format
|
304 |
|
|
|
305 |
def add_message(history, message):
|
306 |
history.append((message, None))
|
307 |
return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
|
@@ -426,83 +464,9 @@ def fetch_local_news():
|
|
426 |
else:
|
427 |
return "<p>Failed to fetch local news</p>"
|
428 |
|
429 |
-
# import numpy as np
|
430 |
-
# import torch
|
431 |
-
# from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
432 |
-
|
433 |
-
# model_id = 'openai/whisper-large-v3'
|
434 |
-
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
435 |
-
# torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
436 |
-
# model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
|
437 |
-
# processor = AutoProcessor.from_pretrained(model_id)
|
438 |
-
|
439 |
-
# 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)
|
440 |
-
|
441 |
-
# base_audio_drive = "/data/audio"
|
442 |
-
|
443 |
-
#Normal Code with sample rate is 44100 Hz
|
444 |
-
|
445 |
-
# def transcribe_function(stream, new_chunk):
|
446 |
-
# try:
|
447 |
-
# sr, y = new_chunk[0], new_chunk[1]
|
448 |
-
# except TypeError:
|
449 |
-
# print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
|
450 |
-
# return stream, "", None
|
451 |
-
|
452 |
-
# y = y.astype(np.float32) / np.max(np.abs(y))
|
453 |
-
|
454 |
-
# if stream is not None:
|
455 |
-
# stream = np.concatenate([stream, y])
|
456 |
-
# else:
|
457 |
-
# stream = y
|
458 |
-
|
459 |
-
# result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
|
460 |
-
|
461 |
-
# full_text = result.get("text","")
|
462 |
-
|
463 |
-
# return stream, full_text, result
|
464 |
-
|
465 |
-
# Resampling code with 16000 Hz
|
466 |
-
|
467 |
-
# import numpy as np
|
468 |
-
# from scipy.signal import resample
|
469 |
-
|
470 |
-
# def transcribe_function(stream, new_chunk):
|
471 |
-
# try:
|
472 |
-
# sr, y = new_chunk[0], new_chunk[1]
|
473 |
-
# except TypeError:
|
474 |
-
# print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
|
475 |
-
# return stream, "", None
|
476 |
-
|
477 |
-
# # Resample to 16000 Hz
|
478 |
-
# target_sr = 16000
|
479 |
-
# if sr != target_sr:
|
480 |
-
# num_samples = int(len(y) * float(target_sr) / sr)
|
481 |
-
# y = resample(y, num_samples)
|
482 |
-
# sr = target_sr
|
483 |
-
|
484 |
-
# y = y.astype(np.float32) / np.max(np.abs(y))
|
485 |
-
|
486 |
-
# if stream is not None:
|
487 |
-
# stream = np.concatenate([stream, y])
|
488 |
-
# else:
|
489 |
-
# stream = y
|
490 |
-
|
491 |
-
# result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
|
492 |
-
|
493 |
-
# full_text = result.get("text", "")
|
494 |
-
|
495 |
-
# return stream, full_text, result
|
496 |
-
|
497 |
-
#Resample part -1
|
498 |
-
|
499 |
import numpy as np
|
500 |
import torch
|
501 |
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
502 |
-
from scipy.signal import resample # Import resample from scipy.signal
|
503 |
-
import base64
|
504 |
-
import io
|
505 |
-
from pydub import AudioSegment
|
506 |
|
507 |
model_id = 'openai/whisper-large-v3'
|
508 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
@@ -512,20 +476,9 @@ processor = AutoProcessor.from_pretrained(model_id)
|
|
512 |
|
513 |
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)
|
514 |
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
base64.b64decode(data)
|
519 |
-
return True
|
520 |
-
return False
|
521 |
-
except Exception:
|
522 |
-
return False
|
523 |
-
|
524 |
-
def base64_to_float32(base64_str):
|
525 |
-
audio_bytes = base64.b64decode(base64_str)
|
526 |
-
audio_segment = AudioSegment.from_file(io.BytesIO(audio_bytes), format="wav")
|
527 |
-
samples = np.array(audio_segment.get_array_of_samples())
|
528 |
-
return audio_segment.frame_rate, samples.astype(np.float32)
|
529 |
|
530 |
def transcribe_function(stream, new_chunk):
|
531 |
try:
|
@@ -534,17 +487,6 @@ def transcribe_function(stream, new_chunk):
|
|
534 |
print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
|
535 |
return stream, "", None
|
536 |
|
537 |
-
# Check if input is base64 and convert to float32 if necessary
|
538 |
-
if is_base64_audio(y):
|
539 |
-
sr, y = base64_to_float32(y)
|
540 |
-
|
541 |
-
# Ensure the sample rate is 16000 Hz
|
542 |
-
target_sr = 16000
|
543 |
-
if sr != target_sr:
|
544 |
-
num_samples = int(len(y) * float(target_sr) / sr)
|
545 |
-
y = resample(y, num_samples)
|
546 |
-
sr = target_sr
|
547 |
-
|
548 |
y = y.astype(np.float32) / np.max(np.abs(y))
|
549 |
|
550 |
if stream is not None:
|
@@ -554,12 +496,14 @@ def transcribe_function(stream, new_chunk):
|
|
554 |
|
555 |
result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
|
556 |
|
557 |
-
full_text = result.get("text",
|
558 |
|
559 |
return stream, full_text, result
|
560 |
|
561 |
|
562 |
|
|
|
|
|
563 |
def update_map_with_response(history):
|
564 |
if not history:
|
565 |
return ""
|
@@ -746,6 +690,35 @@ def update_images():
|
|
746 |
image_3 = generate_image(hardcoded_prompt_3)
|
747 |
return image_1, image_2, image_3
|
748 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
749 |
def fetch_local_events():
|
750 |
api_key = os.environ['SERP_API']
|
751 |
url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Birmingham&hl=en&gl=us&api_key={api_key}'
|
@@ -902,6 +875,90 @@ def handle_retrieval_mode_change(choice):
|
|
902 |
return gr.update(interactive=True), gr.update(interactive=True)
|
903 |
|
904 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
905 |
with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
906 |
with gr.Row():
|
907 |
with gr.Column():
|
@@ -958,7 +1015,8 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
|
958 |
with gr.Column():
|
959 |
weather_output = gr.HTML(value=fetch_local_weather())
|
960 |
news_output = gr.HTML(value=fetch_local_news())
|
961 |
-
events_output = gr.HTML(value=fetch_local_events())
|
|
|
962 |
|
963 |
|
964 |
with gr.Column():
|
|
|
43 |
from langchain_core.messages import AIMessage, HumanMessage
|
44 |
from langchain_core.output_parsers import StrOutputParser
|
45 |
from langchain_core.runnables import RunnableBranch, RunnableLambda, RunnableParallel, RunnablePassthrough
|
46 |
+
from serpapi.google_search import GoogleSearch
|
47 |
|
48 |
# Set environment variables for CUDA
|
49 |
os.environ['PYTORCH_USE_CUDA_DSA'] = '1'
|
|
|
277 |
else:
|
278 |
return "Invalid retrieval mode selected.", []
|
279 |
|
280 |
+
# def bot(history, choice, tts_choice, retrieval_mode):
|
281 |
+
# if not history:
|
282 |
+
# return history
|
283 |
+
|
284 |
+
# response, addresses = generate_answer(history[-1][0], choice, retrieval_mode)
|
285 |
+
# history[-1][1] = ""
|
286 |
+
|
287 |
+
# with concurrent.futures.ThreadPoolExecutor() as executor:
|
288 |
+
# if tts_choice == "Alpha":
|
289 |
+
# audio_future = executor.submit(generate_audio_elevenlabs, response)
|
290 |
+
# elif tts_choice == "Beta":
|
291 |
+
# audio_future = executor.submit(generate_audio_parler_tts, response)
|
292 |
+
# elif tts_choice == "Gamma":
|
293 |
+
# audio_future = executor.submit(generate_audio_mars5, response)
|
294 |
+
|
295 |
+
# for character in response:
|
296 |
+
# history[-1][1] += character
|
297 |
+
# time.sleep(0.05)
|
298 |
+
# yield history, None
|
299 |
+
|
300 |
+
# audio_path = audio_future.result()
|
301 |
+
# yield history, audio_path
|
302 |
+
|
303 |
+
# history.append([response, None]) # Ensure the response is added in the correct format
|
304 |
+
|
305 |
+
|
306 |
def bot(history, choice, tts_choice, retrieval_mode):
|
307 |
if not history:
|
308 |
return history
|
309 |
|
310 |
+
user_message = history[-1][0]
|
311 |
+
response = ""
|
312 |
+
addresses = []
|
313 |
+
|
314 |
+
# Check if the question is about restaurants
|
315 |
+
if "restaurant" in user_message.lower():
|
316 |
+
# Use the agent to get the response
|
317 |
+
response = agent.run(user_message)
|
318 |
+
else:
|
319 |
+
# Otherwise, use the existing logic
|
320 |
+
response, addresses = generate_answer(user_message, choice, retrieval_mode)
|
321 |
+
|
322 |
history[-1][1] = ""
|
323 |
|
324 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
|
339 |
|
340 |
history.append([response, None]) # Ensure the response is added in the correct format
|
341 |
|
342 |
+
|
343 |
def add_message(history, message):
|
344 |
history.append((message, None))
|
345 |
return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
|
|
|
464 |
else:
|
465 |
return "<p>Failed to fetch local news</p>"
|
466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
467 |
import numpy as np
|
468 |
import torch
|
469 |
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
|
|
|
|
|
|
|
|
470 |
|
471 |
model_id = 'openai/whisper-large-v3'
|
472 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
|
476 |
|
477 |
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)
|
478 |
|
479 |
+
base_audio_drive = "/data/audio"
|
480 |
+
|
481 |
+
#Normal Code with sample rate is 44100 Hz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
482 |
|
483 |
def transcribe_function(stream, new_chunk):
|
484 |
try:
|
|
|
487 |
print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
|
488 |
return stream, "", None
|
489 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
490 |
y = y.astype(np.float32) / np.max(np.abs(y))
|
491 |
|
492 |
if stream is not None:
|
|
|
496 |
|
497 |
result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
|
498 |
|
499 |
+
full_text = result.get("text","")
|
500 |
|
501 |
return stream, full_text, result
|
502 |
|
503 |
|
504 |
|
505 |
+
|
506 |
+
|
507 |
def update_map_with_response(history):
|
508 |
if not history:
|
509 |
return ""
|
|
|
690 |
image_3 = generate_image(hardcoded_prompt_3)
|
691 |
return image_1, image_2, image_3
|
692 |
|
693 |
+
|
694 |
+
|
695 |
+
#Crew AI Integration
|
696 |
+
|
697 |
+
from crewai import Agent
|
698 |
+
from langchain.agents import Tool
|
699 |
+
from langchain.utilities import GoogleSerperAPIWrapper
|
700 |
+
|
701 |
+
# Setup API keys
|
702 |
+
os.environ["SERP_API"] = "Your Key"
|
703 |
+
|
704 |
+
search = GoogleSerperAPIWrapper()
|
705 |
+
|
706 |
+
# Create and assign the search tool to an agent
|
707 |
+
serper_tool = Tool(
|
708 |
+
name="Intermediate Answer",
|
709 |
+
func=search.run,
|
710 |
+
description="Useful for search-based queries",
|
711 |
+
)
|
712 |
+
|
713 |
+
agent = Agent(
|
714 |
+
role='Research Analyst',
|
715 |
+
goal='Provide up-to-date market analysis',
|
716 |
+
backstory='An expert analyst with a keen eye for market trends.',
|
717 |
+
tools=[serper_tool]
|
718 |
+
)
|
719 |
+
|
720 |
+
|
721 |
+
|
722 |
def fetch_local_events():
|
723 |
api_key = os.environ['SERP_API']
|
724 |
url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Birmingham&hl=en&gl=us&api_key={api_key}'
|
|
|
875 |
return gr.update(interactive=True), gr.update(interactive=True)
|
876 |
|
877 |
|
878 |
+
def fetch_yelp_restaurants():
|
879 |
+
from serpapi.google_search import GoogleSearch
|
880 |
+
import os
|
881 |
+
|
882 |
+
params = {
|
883 |
+
"engine": "yelp",
|
884 |
+
"find_desc": "Restaurant",
|
885 |
+
"find_loc": "Birmingham, AL, USA",
|
886 |
+
"api_key": os.getenv("SERP_API")
|
887 |
+
}
|
888 |
+
|
889 |
+
search = GoogleSearch(params)
|
890 |
+
results = search.get_dict()
|
891 |
+
organic_results = results.get("organic_results", [])
|
892 |
+
|
893 |
+
yelp_html = """
|
894 |
+
<h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Top Restaurants</h2>
|
895 |
+
<style>
|
896 |
+
table {
|
897 |
+
font-family: 'Verdana', sans-serif;
|
898 |
+
color: #333;
|
899 |
+
border-collapse: collapse;
|
900 |
+
width: 100%;
|
901 |
+
}
|
902 |
+
th, td {
|
903 |
+
border: 1px solid #fff !important;
|
904 |
+
padding: 8px;
|
905 |
+
}
|
906 |
+
th {
|
907 |
+
background-color: #f2f2f2;
|
908 |
+
color: #333;
|
909 |
+
text-align: left;
|
910 |
+
}
|
911 |
+
tr:hover {
|
912 |
+
background-color: #f5f5f5;
|
913 |
+
}
|
914 |
+
.restaurant-link {
|
915 |
+
color: #1E90FF;
|
916 |
+
text-decoration: none;
|
917 |
+
}
|
918 |
+
.restaurant-link:hover {
|
919 |
+
text-decoration: underline;
|
920 |
+
}
|
921 |
+
</style>
|
922 |
+
<table>
|
923 |
+
<tr>
|
924 |
+
<th>Name</th>
|
925 |
+
<th>Rating</th>
|
926 |
+
<th>Reviews</th>
|
927 |
+
<th>Phone</th>
|
928 |
+
<th>Snippet</th>
|
929 |
+
<th>Services</th>
|
930 |
+
</tr>
|
931 |
+
"""
|
932 |
+
for result in organic_results:
|
933 |
+
name = result.get("title", "No name")
|
934 |
+
rating = result.get("rating", "No rating")
|
935 |
+
reviews = result.get("reviews", "No reviews")
|
936 |
+
phone = result.get("phone", "Not Available")
|
937 |
+
snippet = result.get("snippet", "No Available")
|
938 |
+
services = result.get("service_options", "Not Known")
|
939 |
+
|
940 |
+
if isinstance(services, list):
|
941 |
+
services = ", ".join(services)
|
942 |
+
elif isinstance(services, dict):
|
943 |
+
services = ", ".join([f"{key}: {value}" for key, value in services.items()])
|
944 |
+
|
945 |
+
link = result.get("link", "#")
|
946 |
+
|
947 |
+
yelp_html += f"""
|
948 |
+
<tr>
|
949 |
+
<td><a class='restaurant-link' href='{link}' target='_blank'>{name}</a></td>
|
950 |
+
<td>{rating}</td>
|
951 |
+
<td>{reviews}</td>
|
952 |
+
<td>{phone}</td>
|
953 |
+
<td>{snippet}</td>
|
954 |
+
<td>{services}</td>
|
955 |
+
</tr>
|
956 |
+
"""
|
957 |
+
yelp_html += "</table>"
|
958 |
+
return yelp_html
|
959 |
+
|
960 |
+
|
961 |
+
|
962 |
with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
963 |
with gr.Row():
|
964 |
with gr.Column():
|
|
|
1015 |
with gr.Column():
|
1016 |
weather_output = gr.HTML(value=fetch_local_weather())
|
1017 |
news_output = gr.HTML(value=fetch_local_news())
|
1018 |
+
events_output = gr.HTML(value=fetch_local_events())
|
1019 |
+
restaurant_output=gr.HTML(value=fetch_yelp_restaurants())
|
1020 |
|
1021 |
|
1022 |
with gr.Column():
|