Pijush2023 commited on
Commit
f2b6699
·
verified ·
1 Parent(s): 0fee5e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +239 -208
app.py CHANGED
@@ -877,11 +877,6 @@
877
 
878
 
879
 
880
-
881
-
882
-
883
-
884
-
885
  import os
886
  import re
887
  import time
@@ -891,7 +886,6 @@ import folium
891
  import gradio as gr
892
  import tempfile
893
  import torch
894
- import sqlite3
895
  from datetime import datetime
896
  import numpy as np
897
  from gtts import gTTS
@@ -899,8 +893,7 @@ from googlemaps import Client as GoogleMapsClient
899
  from diffusers import StableDiffusion3Pipeline
900
  import concurrent.futures
901
  from PIL import Image
902
- from hashlib import sha256
903
- from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
904
  from langchain_openai import OpenAIEmbeddings, ChatOpenAI
905
  from langchain_pinecone import PineconeVectorStore
906
  from langchain.prompts import PromptTemplate
@@ -909,63 +902,22 @@ from langchain.chains.conversation.memory import ConversationBufferWindowMemory
909
  from langchain.agents import Tool, initialize_agent
910
  from huggingface_hub import login
911
 
912
- # Initialize database
913
- def init_db():
914
- conn = sqlite3.connect("user_data.db")
915
- cursor = conn.cursor()
916
- cursor.execute('''
917
- CREATE TABLE IF NOT EXISTS users (
918
- id INTEGER PRIMARY KEY,
919
- username TEXT UNIQUE NOT NULL,
920
- password TEXT NOT NULL,
921
- created_at TEXT NOT NULL
922
- )
923
- ''')
924
- conn.commit()
925
- conn.close()
926
-
927
- init_db()
928
-
929
- def hash_password(password):
930
- return sha256(password.encode()).hexdigest()
931
-
932
- def verify_password(stored_password, provided_password):
933
- return stored_password == sha256(provided_password.encode()).hexdigest()
934
-
935
- def create_account(username, password):
936
- conn = sqlite3.connect("user_data.db")
937
- cursor = conn.cursor()
938
- try:
939
- cursor.execute('''
940
- INSERT INTO users (username, password, created_at) VALUES (?, ?, ?)
941
- ''', (username, hash_password(password), datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
942
- conn.commit()
943
- conn.close()
944
- return "Account created successfully!"
945
- except sqlite3.IntegrityError:
946
- conn.close()
947
- return "Username already exists!"
948
-
949
- def login_user(username, password):
950
- conn = sqlite3.connect("user_data.db")
951
- cursor = conn.cursor()
952
- cursor.execute('''
953
- SELECT password FROM users WHERE username = ?
954
- ''', (username,))
955
- stored_password = cursor.fetchone()
956
- conn.close()
957
- if stored_password and verify_password(stored_password[0], password):
958
- return True
959
- else:
960
- return False
961
 
962
  # Check if the token is already set in the environment variables
963
  hf_token = os.getenv("HF_TOKEN")
 
964
  if hf_token is None:
 
965
  print("Please set your Hugging Face token in the environment variables.")
966
  else:
 
967
  login(token=hf_token)
968
 
 
 
 
969
  # Set up logging
970
  logging.basicConfig(level=logging.DEBUG)
971
 
@@ -994,128 +946,13 @@ def get_current_time_and_date():
994
  now = datetime.now()
995
  return now.strftime("%Y-%m-%d %H:%M:%S")
996
 
 
997
  current_time_and_date = get_current_time_and_date()
998
 
999
- template1 = """You are an expert concierge who is helpful and a renowned guide for Omaha, Nebraska. Based on weather being a sunny bright day and the today's date is 20th june 2024, use the following pieces of context,
1000
- memory, and message history, along with your knowledge of perennial events in Omaha, Nebraska, 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.
1001
- Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and
1002
- event type and description. Always say "It was my pleasure!" at the end of the answer.
1003
- {context}
1004
- Question: {question}
1005
- Helpful Answer:"""
1006
-
1007
- template2 = """You are an expert concierge who is helpful and a renowned guide for Omaha, Nebraska. Based on today's weather being a sunny bright day and today's date is 20th june 2024, take the location or address but don't show the location or address on the output prompts. Use the following pieces of context,
1008
- memory, and message history, along with your knowledge of perennial events in Omaha, Nebraska, 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.
1009
- Keep the answer short and sweet and crisp. Always say "It was my pleasure!" at the end of the answer.
1010
- {context}
1011
- Question: {question}
1012
- Helpful Answer:"""
1013
-
1014
- QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
1015
- QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
1016
-
1017
- def build_qa_chain(prompt_template):
1018
- qa_chain = RetrievalQA.from_chain_type(
1019
- llm=chat_model,
1020
- chain_type="stuff",
1021
- retriever=retriever,
1022
- chain_type_kwargs={"prompt": prompt_template}
1023
- )
1024
- tools = [
1025
- Tool(
1026
- name='Knowledge Base',
1027
- func=qa_chain,
1028
- description='Use this tool when answering general knowledge queries to get more information about the topic'
1029
- )
1030
- ]
1031
- return qa_chain, tools
1032
-
1033
- def initialize_agent_with_prompt(prompt_template):
1034
- qa_chain, tools = build_qa_chain(prompt_template)
1035
- agent = initialize_agent(
1036
- agent='chat-conversational-react-description',
1037
- tools=tools,
1038
- llm=chat_model,
1039
- verbose=False,
1040
- max_iteration=5,
1041
- early_stopping_method='generate',
1042
- memory=conversational_memory
1043
- )
1044
- return agent
1045
-
1046
- def generate_answer(message, choice):
1047
- logging.debug(f"generate_answer called with prompt_choice: {choice}")
1048
- if choice == "Details":
1049
- agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_1)
1050
- elif choice == "Conversational":
1051
- agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
1052
- else:
1053
- logging.error(f"Invalid prompt_choice: {choice}. Defaulting to 'Conversational'")
1054
- agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
1055
- response = agent(message)
1056
- addresses = extract_addresses(response['output'])
1057
- return response['output'], addresses
1058
-
1059
- def bot(history, choice):
1060
- if not history:
1061
- return history
1062
- response, addresses = generate_answer(history[-1][0], choice)
1063
- history[-1][1] = ""
1064
- with concurrent.futures.ThreadPoolExecutor() as executor:
1065
- audio_future = executor.submit(generate_audio_elevenlabs, response)
1066
- for character in response:
1067
- history[-1][1] += character
1068
- time.sleep(0.05)
1069
- yield history, None
1070
- audio_path = audio_future.result()
1071
- yield history, audio_path
1072
-
1073
- def add_message(history, message):
1074
- history.append((message, None))
1075
- return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
1076
-
1077
- def print_like_dislike(x: gr.LikeData):
1078
- print(x.index, x.value, x.liked)
1079
-
1080
- def extract_addresses(response):
1081
- if not isinstance(response, str):
1082
- response = str(response)
1083
- address_patterns = [
1084
- r'([A-Z].*,\sOmaha,\sNE\s\d{5})',
1085
- r'(\d{4}\s.*,\sOmaha,\sNE\s\d{5})',
1086
- r'([A-Z].*,\sNE\s\d{5})',
1087
- r'([A-Z].*,.*\sSt,\sOmaha,\sNE\s\d{5})',
1088
- r'([A-Z].*,.*\sStreets,\sOmaha,\sNE\s\d{5})',
1089
- r'(\d{2}.*\sStreets)',
1090
- r'([A-Z].*\s\d{2},\sOmaha,\sNE\s\d{5})'
1091
- ]
1092
- addresses = []
1093
- for pattern in address_patterns:
1094
- addresses.extend(re.findall(pattern, response))
1095
- return addresses
1096
-
1097
- all_addresses = []
1098
-
1099
- def generate_map(location_names):
1100
- global all_addresses
1101
- all_addresses.extend(location_names)
1102
- api_key = os.environ['GOOGLEMAPS_API_KEY']
1103
- gmaps = GoogleMapsClient(key=api_key)
1104
- m = folium.Map(location=[41.2565, -95.9345], zoom_start=12)
1105
- for location_name in all_addresses:
1106
- geocode_result = gmaps.geocode(location_name)
1107
- if geocode_result:
1108
- location = geocode_result[0]['geometry']['location']
1109
- folium.Marker(
1110
- [location['lat'], location['lng']],
1111
- tooltip=f"{geocode_result[0]['formatted_address']}"
1112
- ).add_to(m)
1113
- map_html = m._repr_html_()
1114
- return map_html
1115
-
1116
  def fetch_local_events():
1117
  api_key = os.environ['SERP_API']
1118
  url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Omaha&hl=en&gl=us&api_key={api_key}'
 
1119
  response = requests.get(url)
1120
  if response.status_code == 200:
1121
  events_results = response.json().get("events_results", [])
@@ -1160,14 +997,18 @@ def fetch_local_weather():
1160
  response = requests.get(url)
1161
  response.raise_for_status()
1162
  jsonData = response.json()
 
1163
  current_conditions = jsonData.get("currentConditions", {})
1164
  temp_celsius = current_conditions.get("temp", "N/A")
 
1165
  if temp_celsius != "N/A":
1166
  temp_fahrenheit = int((temp_celsius * 9/5) + 32)
1167
  else:
1168
  temp_fahrenheit = "N/A"
 
1169
  condition = current_conditions.get("conditions", "N/A")
1170
  humidity = current_conditions.get("humidity", "N/A")
 
1171
  weather_html = f"""
1172
  <div class="weather-theme">
1173
  <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Weather</h2>
@@ -1308,30 +1149,45 @@ def fetch_local_news():
1308
  else:
1309
  return "<p>Failed to fetch local news</p>"
1310
 
 
 
 
 
 
1311
  model_id = 'openai/whisper-large-v3'
1312
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
1313
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
1314
  model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype,
1315
  use_safetensors=True).to(device)
1316
  processor = AutoProcessor.from_pretrained(model_id)
 
 
1317
  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)
1318
 
1319
  base_audio_drive = "/data/audio"
1320
 
 
 
1321
  def transcribe_function(stream, new_chunk):
1322
  try:
1323
  sr, y = new_chunk[0], new_chunk[1]
1324
  except TypeError:
1325
  print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
1326
  return stream, "", None
 
1327
  y = y.astype(np.float32) / np.max(np.abs(y))
 
1328
  if stream is not None:
1329
  stream = np.concatenate([stream, y])
1330
  else:
1331
  stream = y
 
1332
  result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
 
1333
  full_text = result.get("text", "")
 
1334
  return stream, full_text, result
 
1335
 
1336
  def update_map_with_response(history):
1337
  if not history:
@@ -1341,17 +1197,17 @@ def update_map_with_response(history):
1341
  return generate_map(addresses)
1342
 
1343
  def clear_textbox():
1344
- return ""
1345
 
1346
- def show_map_if_details(history, choice):
1347
  if choice in ["Details", "Conversational"]:
1348
  return gr.update(visible=True), update_map_with_response(history)
1349
  else:
1350
- return gr.update(visible=False), ""
1351
 
1352
  def generate_audio_elevenlabs(text):
1353
  XI_API_KEY = os.environ['ELEVENLABS_API']
1354
- VOICE_ID = 'd9MIrwLnvDeH7aZb61E9'
1355
  tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
1356
  headers = {
1357
  "Accept": "application/json",
@@ -1363,7 +1219,7 @@ def generate_audio_elevenlabs(text):
1363
  "voice_settings": {
1364
  "stability": 1.0,
1365
  "similarity_boost": 0.0,
1366
- "style": 0.60,
1367
  "use_speaker_boost": False
1368
  }
1369
  }
@@ -1379,6 +1235,7 @@ def generate_audio_elevenlabs(text):
1379
  logging.error(f"Error generating audio: {response.text}")
1380
  return None
1381
 
 
1382
  pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
1383
  pipe = pipe.to("cuda")
1384
 
@@ -1391,7 +1248,8 @@ def generate_image(prompt):
1391
  ).images[0]
1392
  return image
1393
 
1394
- hardcoded_prompt_1="Give a high quality photograph of a great looking red 2026 toyota coupe against a skyline setting in th night, michael mann style in omaha enticing the consumer to buy this product"
 
1395
  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."
1396
  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."
1397
 
@@ -1401,39 +1259,204 @@ def update_images():
1401
  image_3 = generate_image(hardcoded_prompt_3)
1402
  return image_1, image_2, image_3
1403
 
1404
- authenticated = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1405
 
1406
- def check_login(username, password):
1407
- global authenticated
1408
  if login_user(username, password):
1409
- authenticated = True
1410
- return "Login successful! Redirecting...", gr.update(visible=False), gr.update(visible=True)
1411
  else:
1412
- return "Invalid username or password.", gr.update(visible=True), gr.update(visible=False)
1413
 
1414
- with gr.Blocks() as demo:
1415
- state = gr.State()
1416
- with gr.Row(visible=not authenticated) as login_row:
1417
- with gr.Column():
1418
- gr.Markdown("### Login")
1419
- username = gr.Textbox(label="Username")
1420
- password = gr.Textbox(label="Password", type="password")
1421
- login_button = gr.Button("Login")
1422
- login_output = gr.Markdown()
1423
- login_button.click(fn=check_login, inputs=[username, password], outputs=[login_output, login_row, gr.update(visible=True)])
 
 
 
 
 
1424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1425
  with gr.Column():
1426
- gr.Markdown("### Create Account")
1427
- username_create = gr.Textbox(label="Username")
1428
- password_create = gr.Textbox(label="Password", type="password")
1429
- create_button = gr.Button("Create Account")
1430
- create_output = gr.Markdown()
1431
- create_button.click(fn=create_account, inputs=[username_create, password_create], outputs=create_output)
1432
-
1433
- with gr.Row(visible=authenticated) as main_interface:
1434
- with gr.Column():
1435
  chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
1436
  choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
 
1437
  gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
1438
  chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
1439
  chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
@@ -1442,24 +1465,32 @@ with gr.Blocks() as demo:
1442
  chatbot.like(print_like_dislike, None, None)
1443
  clear_button = gr.Button("Clear")
1444
  clear_button.click(fn=clear_textbox, inputs=None, outputs=chat_input)
 
 
1445
  audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy')
1446
  audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="SAMLOne_real_time")
 
1447
  gr.Markdown("<h1 style='color: red;'>Map</h1>", elem_id="location-markdown")
1448
  location_output = gr.HTML()
1449
  bot_msg.then(show_map_if_details, [chatbot, choice], [location_output, location_output])
1450
-
1451
  with gr.Column():
1452
  weather_output = gr.HTML(value=fetch_local_weather())
1453
  news_output = gr.HTML(value=fetch_local_news())
1454
- events_output = gr.HTML(value=fetch_local_events())
1455
-
1456
  with gr.Column():
1457
  image_output_1 = gr.Image(value=generate_image(hardcoded_prompt_1), width=400, height=400)
1458
  image_output_2 = gr.Image(value=generate_image(hardcoded_prompt_2), width=400, height=400)
1459
  image_output_3 = gr.Image(value=generate_image(hardcoded_prompt_3), width=400, height=400)
 
1460
  refresh_button = gr.Button("Refresh Images")
1461
  refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
1462
 
1463
  demo.queue()
1464
  demo.launch(share=True)
1465
 
 
 
 
 
 
877
 
878
 
879
 
 
 
 
 
 
880
  import os
881
  import re
882
  import time
 
886
  import gradio as gr
887
  import tempfile
888
  import torch
 
889
  from datetime import datetime
890
  import numpy as np
891
  from gtts import gTTS
 
893
  from diffusers import StableDiffusion3Pipeline
894
  import concurrent.futures
895
  from PIL import Image
896
+
 
897
  from langchain_openai import OpenAIEmbeddings, ChatOpenAI
898
  from langchain_pinecone import PineconeVectorStore
899
  from langchain.prompts import PromptTemplate
 
902
  from langchain.agents import Tool, initialize_agent
903
  from huggingface_hub import login
904
 
905
+ import sqlite3
906
+ import hashlib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
907
 
908
  # Check if the token is already set in the environment variables
909
  hf_token = os.getenv("HF_TOKEN")
910
+
911
  if hf_token is None:
912
+ # If the token is not set, prompt for it (this should be done securely)
913
  print("Please set your Hugging Face token in the environment variables.")
914
  else:
915
+ # Login using the token
916
  login(token=hf_token)
917
 
918
+ # Your application logic goes here
919
+ print("Logged in successfully to Hugging Face Hub!")
920
+
921
  # Set up logging
922
  logging.basicConfig(level=logging.DEBUG)
923
 
 
946
  now = datetime.now()
947
  return now.strftime("%Y-%m-%d %H:%M:%S")
948
 
949
+ # Example usage
950
  current_time_and_date = get_current_time_and_date()
951
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
952
  def fetch_local_events():
953
  api_key = os.environ['SERP_API']
954
  url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Omaha&hl=en&gl=us&api_key={api_key}'
955
+
956
  response = requests.get(url)
957
  if response.status_code == 200:
958
  events_results = response.json().get("events_results", [])
 
997
  response = requests.get(url)
998
  response.raise_for_status()
999
  jsonData = response.json()
1000
+
1001
  current_conditions = jsonData.get("currentConditions", {})
1002
  temp_celsius = current_conditions.get("temp", "N/A")
1003
+
1004
  if temp_celsius != "N/A":
1005
  temp_fahrenheit = int((temp_celsius * 9/5) + 32)
1006
  else:
1007
  temp_fahrenheit = "N/A"
1008
+
1009
  condition = current_conditions.get("conditions", "N/A")
1010
  humidity = current_conditions.get("humidity", "N/A")
1011
+
1012
  weather_html = f"""
1013
  <div class="weather-theme">
1014
  <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Weather</h2>
 
1149
  else:
1150
  return "<p>Failed to fetch local news</p>"
1151
 
1152
+ # Voice Control
1153
+ import numpy as np
1154
+ import torch
1155
+ from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
1156
+
1157
  model_id = 'openai/whisper-large-v3'
1158
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
1159
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
1160
  model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype,
1161
  use_safetensors=True).to(device)
1162
  processor = AutoProcessor.from_pretrained(model_id)
1163
+
1164
+ # Optimized ASR pipeline
1165
  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)
1166
 
1167
  base_audio_drive = "/data/audio"
1168
 
1169
+ import numpy as np
1170
+
1171
  def transcribe_function(stream, new_chunk):
1172
  try:
1173
  sr, y = new_chunk[0], new_chunk[1]
1174
  except TypeError:
1175
  print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
1176
  return stream, "", None
1177
+
1178
  y = y.astype(np.float32) / np.max(np.abs(y))
1179
+
1180
  if stream is not None:
1181
  stream = np.concatenate([stream, y])
1182
  else:
1183
  stream = y
1184
+
1185
  result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
1186
+
1187
  full_text = result.get("text", "")
1188
+
1189
  return stream, full_text, result
1190
+
1191
 
1192
  def update_map_with_response(history):
1193
  if not history:
 
1197
  return generate_map(addresses)
1198
 
1199
  def clear_textbox():
1200
+ return ""
1201
 
1202
+ def show_map_if_details(history,choice):
1203
  if choice in ["Details", "Conversational"]:
1204
  return gr.update(visible=True), update_map_with_response(history)
1205
  else:
1206
+ return gr.update(visible(False), "")
1207
 
1208
  def generate_audio_elevenlabs(text):
1209
  XI_API_KEY = os.environ['ELEVENLABS_API']
1210
+ VOICE_ID = 'd9MIrwLnvDeH7aZb61E9' # Replace with your voice ID
1211
  tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
1212
  headers = {
1213
  "Accept": "application/json",
 
1219
  "voice_settings": {
1220
  "stability": 1.0,
1221
  "similarity_boost": 0.0,
1222
+ "style": 0.60, # Adjust style for more romantic tone
1223
  "use_speaker_boost": False
1224
  }
1225
  }
 
1235
  logging.error(f"Error generating audio: {response.text}")
1236
  return None
1237
 
1238
+ # Stable Diffusion setup
1239
  pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
1240
  pipe = pipe.to("cuda")
1241
 
 
1248
  ).images[0]
1249
  return image
1250
 
1251
+ # Hardcoded prompt for image generation
1252
+ hardcoded_prompt_1="Give a high quality photograph of a great looking red 2026 toyota coupe against a skyline setting in the night, michael mann style in omaha enticing the consumer to buy this product"
1253
  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."
1254
  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."
1255
 
 
1259
  image_3 = generate_image(hardcoded_prompt_3)
1260
  return image_1, image_2, image_3
1261
 
1262
+ # Initialize database and create user table if not exists
1263
+ def init_db():
1264
+ conn = sqlite3.connect('user_data.db')
1265
+ c = conn.cursor()
1266
+ c.execute('''CREATE TABLE IF NOT EXISTS users
1267
+ (username TEXT PRIMARY KEY, password TEXT)''')
1268
+ conn.commit()
1269
+ conn.close()
1270
+
1271
+ init_db()
1272
+
1273
+ def hash_password(password):
1274
+ return hashlib.sha256(password.encode()).hexdigest()
1275
+
1276
+ def signup_user(username, password):
1277
+ conn = sqlite3.connect('user_data.db')
1278
+ c = conn.cursor()
1279
+ try:
1280
+ c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hash_password(password)))
1281
+ conn.commit()
1282
+ return True
1283
+ except sqlite3.IntegrityError:
1284
+ return False
1285
+ finally:
1286
+ conn.close()
1287
+
1288
+ def login_user(username, password):
1289
+ conn = sqlite3.connect('user_data.db')
1290
+ c = conn.cursor()
1291
+ c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, hash_password(password)))
1292
+ user = c.fetchone()
1293
+ conn.close()
1294
+ return user is not None
1295
+
1296
+ def handle_signup(username, password):
1297
+ if '@mangoes.ai' not in username:
1298
+ return "Username must include '@mangoes.ai'."
1299
+ if signup_user(username, password):
1300
+ return "Signup successful! You can now log in."
1301
+ else:
1302
+ return "Signup failed! Username may already be taken."
1303
 
1304
+ def handle_login(username, password):
 
1305
  if login_user(username, password):
1306
+ return "Login successful!"
 
1307
  else:
1308
+ return "Login failed! Please check your credentials."
1309
 
1310
+ def build_qa_chain(prompt_template):
1311
+ qa_chain = RetrievalQA.from_chain_type(
1312
+ llm=chat_model,
1313
+ chain_type="stuff",
1314
+ retriever=retriever,
1315
+ chain_type_kwargs={"prompt": prompt_template}
1316
+ )
1317
+ tools = [
1318
+ Tool(
1319
+ name='Knowledge Base',
1320
+ func=qa_chain,
1321
+ description='Use this tool when answering general knowledge queries to get more information about the topic'
1322
+ )
1323
+ ]
1324
+ return qa_chain, tools
1325
 
1326
+ def initialize_agent_with_prompt(prompt_template):
1327
+ qa_chain, tools = build_qa_chain(prompt_template)
1328
+ agent = initialize_agent(
1329
+ agent='chat-conversational-react-description',
1330
+ tools=tools,
1331
+ llm=chat_model,
1332
+ verbose=False,
1333
+ max_iteration=5,
1334
+ early_stopping_method='generate',
1335
+ memory=conversational_memory
1336
+ )
1337
+ return agent
1338
+
1339
+ def generate_answer(message, choice):
1340
+ logging.debug(f"generate_answer called with prompt_choice: {choice}")
1341
+
1342
+ if choice == "Details":
1343
+ agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_1)
1344
+ elif choice == "Conversational":
1345
+ agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
1346
+ else:
1347
+ logging.error(f"Invalid prompt_choice: {choice}. Defaulting to 'Conversational'")
1348
+ agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
1349
+ response = agent(message)
1350
+
1351
+ # Extract addresses for mapping regardless of the choice
1352
+ addresses = extract_addresses(response['output'])
1353
+ return response['output'], addresses
1354
+
1355
+ def bot(history, choice):
1356
+ if not history:
1357
+ return history
1358
+ response, addresses = generate_answer(history[-1][0], choice)
1359
+ history[-1][1] = ""
1360
+
1361
+ # Generate audio for the entire response in a separate thread
1362
+ with concurrent.futures.ThreadPoolExecutor() as executor:
1363
+ audio_future = executor.submit(generate_audio_elevenlabs, response)
1364
+
1365
+ for character in response:
1366
+ history[-1][1] += character
1367
+ time.sleep(0.05) # Adjust the speed of text appearance
1368
+ yield history, None
1369
+
1370
+ audio_path = audio_future.result()
1371
+ yield history, audio_path
1372
+
1373
+ def add_message(history, message):
1374
+ history.append((message, None))
1375
+ return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
1376
+
1377
+ def print_like_dislike(x: gr.LikeData):
1378
+ print(x.index, x.value, x.liked)
1379
+
1380
+ def extract_addresses(response):
1381
+ if not isinstance(response, str):
1382
+ response = str(response)
1383
+ address_patterns = [
1384
+ r'([A-Z].*,\sOmaha,\sNE\s\d{5})',
1385
+ r'(\d{4}\s.*,\sOmaha,\sNE\s\d{5})',
1386
+ r'([A-Z].*,\sNE\s\d{5})',
1387
+ r'([A-Z].*,.*\sSt,\sOmaha,\sNE\s\d{5})',
1388
+ r'([A-Z].*,.*\sStreets,\sOmaha,\sNE\s\d{5})',
1389
+ r'(\d{2}.*\sStreets)',
1390
+ r'([A-Z].*\s\d{2},\sOmaha,\sNE\s\d{5})'
1391
+ ]
1392
+ addresses = []
1393
+ for pattern in address_patterns:
1394
+ addresses.extend(re.findall(pattern, response))
1395
+ return addresses
1396
+
1397
+ all_addresses = []
1398
+
1399
+ def generate_map(location_names):
1400
+ global all_addresses
1401
+ all_addresses.extend(location_names)
1402
+
1403
+ api_key = os.environ['GOOGLEMAPS_API_KEY']
1404
+ gmaps = GoogleMapsClient(key=api_key)
1405
+
1406
+ m = folium.Map(location=[41.2565, -95.9345], zoom_start=12)
1407
+
1408
+ for location_name in all_addresses:
1409
+ geocode_result = gmaps.geocode(location_name)
1410
+ if geocode_result:
1411
+ location = geocode_result[0]['geometry']['location']
1412
+ folium.Marker(
1413
+ [location['lat'], location['lng']],
1414
+ tooltip=f"{geocode_result[0]['formatted_address']}"
1415
+ ).add_to(m)
1416
+
1417
+ map_html = m._repr_html_()
1418
+ return map_html
1419
+
1420
+ template1 = """You are an expert concierge who is helpful and a renowned guide for Omaha, Nebraska. Based on weather being a sunny bright day and the today's date is 20th june 2024, use the following pieces of context,
1421
+ memory, and message history, along with your knowledge of perennial events in Omaha, Nebraska, 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.
1422
+ Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and
1423
+ event type and description. Always say "It was my pleasure!" at the end of the answer.
1424
+ {context}
1425
+ Question: {question}
1426
+ Helpful Answer:"""
1427
+
1428
+ template2 = """You are an expert concierge who is helpful and a renowned guide for Omaha, Nebraska. Based on today's weather being a sunny bright day and today's date is 20th june 2024, take the location or address but don't show the location or address on the output prompts. Use the following pieces of context,
1429
+ memory, and message history, along with your knowledge of perennial events in Omaha, Nebraska, 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.
1430
+ Keep the answer short and sweet and crisp. Always say "It was my pleasure!" at the end of the answer.
1431
+ {context}
1432
+ Question: {question}
1433
+ Helpful Answer:"""
1434
+
1435
+ QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
1436
+ QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
1437
+
1438
+ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
1439
+ with gr.Tab("Login"):
1440
+ login_username = gr.Textbox(label="Username")
1441
+ login_password = gr.Password(label="Password")
1442
+ login_button = gr.Button("Login")
1443
+ login_output = gr.Textbox(label="Login Status", interactive=False)
1444
+ login_button.click(handle_login, inputs=[login_username, login_password], outputs=login_output)
1445
+
1446
+ with gr.Tab("Signup"):
1447
+ signup_username = gr.Textbox(label="Username")
1448
+ signup_password = gr.Password(label="Password")
1449
+ signup_button = gr.Button("Signup")
1450
+ signup_output = gr.Textbox(label="Signup Status", interactive=False)
1451
+ signup_button.click(handle_signup, inputs=[signup_username, signup_password], outputs=signup_output)
1452
+
1453
+ with gr.Row():
1454
  with gr.Column():
1455
+ state = gr.State()
1456
+
 
 
 
 
 
 
 
1457
  chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
1458
  choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
1459
+
1460
  gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
1461
  chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
1462
  chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
 
1465
  chatbot.like(print_like_dislike, None, None)
1466
  clear_button = gr.Button("Clear")
1467
  clear_button.click(fn=clear_textbox, inputs=None, outputs=chat_input)
1468
+
1469
+
1470
  audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy')
1471
  audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="SAMLOne_real_time")
1472
+
1473
  gr.Markdown("<h1 style='color: red;'>Map</h1>", elem_id="location-markdown")
1474
  location_output = gr.HTML()
1475
  bot_msg.then(show_map_if_details, [chatbot, choice], [location_output, location_output])
1476
+
1477
  with gr.Column():
1478
  weather_output = gr.HTML(value=fetch_local_weather())
1479
  news_output = gr.HTML(value=fetch_local_news())
1480
+ news_output = gr.HTML(value=fetch_local_events())
1481
+
1482
  with gr.Column():
1483
  image_output_1 = gr.Image(value=generate_image(hardcoded_prompt_1), width=400, height=400)
1484
  image_output_2 = gr.Image(value=generate_image(hardcoded_prompt_2), width=400, height=400)
1485
  image_output_3 = gr.Image(value=generate_image(hardcoded_prompt_3), width=400, height=400)
1486
+
1487
  refresh_button = gr.Button("Refresh Images")
1488
  refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
1489
 
1490
  demo.queue()
1491
  demo.launch(share=True)
1492
 
1493
+
1494
+
1495
+
1496
+