Pijush2023 commited on
Commit
f79c471
·
verified ·
1 Parent(s): d00a860

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +194 -259
app.py CHANGED
@@ -882,17 +882,6 @@
882
 
883
 
884
 
885
-
886
-
887
-
888
-
889
-
890
-
891
-
892
-
893
-
894
-
895
-
896
  import os
897
  import re
898
  import time
@@ -903,7 +892,6 @@ import gradio as gr
903
  import tempfile
904
  import torch
905
  import sqlite3
906
- import hashlib
907
  from datetime import datetime
908
  import numpy as np
909
  from gtts import gTTS
@@ -911,7 +899,8 @@ from googlemaps import Client as GoogleMapsClient
911
  from diffusers import StableDiffusion3Pipeline
912
  import concurrent.futures
913
  from PIL import Image
914
-
 
915
  from langchain_openai import OpenAIEmbeddings, ChatOpenAI
916
  from langchain_pinecone import PineconeVectorStore
917
  from langchain.prompts import PromptTemplate
@@ -920,61 +909,63 @@ from langchain.chains.conversation.memory import ConversationBufferWindowMemory
920
  from langchain.agents import Tool, initialize_agent
921
  from huggingface_hub import login
922
 
923
- # Create a new SQLite database (or connect to an existing one)
924
- conn = sqlite3.connect('users.db')
925
- cursor = conn.cursor()
 
 
 
 
 
 
 
 
 
 
 
926
 
927
- # Create a table for storing user credentials
928
- cursor.execute('''
929
- CREATE TABLE IF NOT EXISTS users (
930
- id INTEGER PRIMARY KEY AUTOINCREMENT,
931
- username TEXT NOT NULL UNIQUE,
932
- password TEXT NOT NULL
933
- )
934
- ''')
935
- conn.commit()
936
- conn.close()
937
 
938
- # Helper functions for user authentication
939
  def hash_password(password):
940
- return hashlib.sha256(password.encode()).hexdigest()
 
 
 
941
 
942
- def register_user(username, password):
943
- conn = sqlite3.connect('users.db')
944
  cursor = conn.cursor()
945
  try:
946
- cursor.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, hash_password(password)))
 
 
947
  conn.commit()
948
- return True, "User registered successfully!"
 
949
  except sqlite3.IntegrityError:
950
- return False, "Username already exists!"
951
- finally:
952
  conn.close()
 
953
 
954
- def authenticate_user(username, password):
955
- conn = sqlite3.connect('users.db')
956
  cursor = conn.cursor()
957
- cursor.execute('SELECT password FROM users WHERE username = ?', (username,))
958
- record = cursor.fetchone()
 
 
959
  conn.close()
960
- if record and record[0] == hash_password(password):
961
- return True, "Login successful!"
962
  else:
963
- return False, "Invalid username or password!"
964
 
965
  # Check if the token is already set in the environment variables
966
  hf_token = os.getenv("HF_TOKEN")
967
-
968
  if hf_token is None:
969
- # If the token is not set, prompt for it (this should be done securely)
970
  print("Please set your Hugging Face token in the environment variables.")
971
  else:
972
- # Login using the token
973
  login(token=hf_token)
974
 
975
- # Your application logic goes here
976
- print("Logged in successfully to Hugging Face Hub!")
977
-
978
  # Set up logging
979
  logging.basicConfig(level=logging.DEBUG)
980
 
@@ -1003,13 +994,128 @@ def get_current_time_and_date():
1003
  now = datetime.now()
1004
  return now.strftime("%Y-%m-%d %H:%M:%S")
1005
 
1006
- # Example usage
1007
  current_time_and_date = get_current_time_and_date()
1008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1009
  def fetch_local_events():
1010
  api_key = os.environ['SERP_API']
1011
  url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Omaha&hl=en&gl=us&api_key={api_key}'
1012
-
1013
  response = requests.get(url)
1014
  if response.status_code == 200:
1015
  events_results = response.json().get("events_results", [])
@@ -1054,18 +1160,14 @@ def fetch_local_weather():
1054
  response = requests.get(url)
1055
  response.raise_for_status()
1056
  jsonData = response.json()
1057
-
1058
  current_conditions = jsonData.get("currentConditions", {})
1059
  temp_celsius = current_conditions.get("temp", "N/A")
1060
-
1061
  if temp_celsius != "N/A":
1062
  temp_fahrenheit = int((temp_celsius * 9/5) + 32)
1063
  else:
1064
  temp_fahrenheit = "N/A"
1065
-
1066
  condition = current_conditions.get("conditions", "N/A")
1067
  humidity = current_conditions.get("humidity", "N/A")
1068
-
1069
  weather_html = f"""
1070
  <div class="weather-theme">
1071
  <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Weather</h2>
@@ -1132,138 +1234,6 @@ def get_weather_icon(condition):
1132
  }
1133
  return condition_map.get(condition, "c04d")
1134
 
1135
- # Define prompt templates
1136
- 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,
1137
- 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.
1138
- Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and
1139
- event type and description. Always say "It was my pleasure!" at the end of the answer.
1140
- {context}
1141
- Question: {question}
1142
- Helpful Answer:"""
1143
-
1144
- 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,
1145
- 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.
1146
- Keep the answer short and sweet and crisp. Always say "It was my pleasure!" at the end of the answer.
1147
- {context}
1148
- Question: {question}
1149
- Helpful Answer:"""
1150
-
1151
- QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
1152
- QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
1153
-
1154
- # Define the retrieval QA chain
1155
- def build_qa_chain(prompt_template):
1156
- qa_chain = RetrievalQA.from_chain_type(
1157
- llm=chat_model,
1158
- chain_type="stuff",
1159
- retriever=retriever,
1160
- chain_type_kwargs={"prompt": prompt_template}
1161
- )
1162
- tools = [
1163
- Tool(
1164
- name='Knowledge Base',
1165
- func=qa_chain,
1166
- description='Use this tool when answering general knowledge queries to get more information about the topic'
1167
- )
1168
- ]
1169
- return qa_chain, tools
1170
-
1171
- # Define the agent initializer
1172
- def initialize_agent_with_prompt(prompt_template):
1173
- qa_chain, tools = build_qa_chain(prompt_template)
1174
- agent = initialize_agent(
1175
- agent='chat-conversational-react-description',
1176
- tools=tools,
1177
- llm=chat_model,
1178
- verbose=False,
1179
- max_iteration=5,
1180
- early_stopping_method='generate',
1181
- memory=conversational_memory
1182
- )
1183
- return agent
1184
-
1185
- # Define the function to generate answers
1186
- def generate_answer(message, choice):
1187
- logging.debug(f"generate_answer called with prompt_choice: {choice}")
1188
-
1189
- if choice == "Details":
1190
- agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_1)
1191
- elif choice == "Conversational":
1192
- agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
1193
- else:
1194
- logging.error(f"Invalid prompt_choice: {choice}. Defaulting to 'Conversational'")
1195
- agent = initialize_agent_with_prompt(QA_CHAIN_PROMPT_2)
1196
- response = agent(message)
1197
-
1198
- # Extract addresses for mapping regardless of the choice
1199
- addresses = extract_addresses(response['output'])
1200
- return response['output'], addresses
1201
-
1202
- def bot(history, choice):
1203
- if not history:
1204
- return history
1205
- response, addresses = generate_answer(history[-1][0], choice)
1206
- history[-1][1] = ""
1207
-
1208
- # Generate audio for the entire response in a separate thread
1209
- with concurrent.futures.ThreadPoolExecutor() as executor:
1210
- audio_future = executor.submit(generate_audio_elevenlabs, response)
1211
-
1212
- for character in response:
1213
- history[-1][1] += character
1214
- time.sleep(0.05) # Adjust the speed of text appearance
1215
- yield history, None
1216
-
1217
- audio_path = audio_future.result()
1218
- yield history, audio_path
1219
-
1220
- def add_message(history, message):
1221
- history.append((message, None))
1222
- return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
1223
-
1224
- def print_like_dislike(x: gr.LikeData):
1225
- print(x.index, x.value, x.liked)
1226
-
1227
- def extract_addresses(response):
1228
- if not isinstance(response, str):
1229
- response = str(response)
1230
- address_patterns = [
1231
- r'([A-Z].*,\sOmaha,\sNE\s\d{5})',
1232
- r'(\d{4}\s.*,\sOmaha,\sNE\s\d{5})',
1233
- r'([A-Z].*,\sNE\s\d{5})',
1234
- r'([A-Z].*,.*\sSt,\sOmaha,\sNE\s\d{5})',
1235
- r'([A-Z].*,.*\sStreets,\sOmaha,\sNE\s\d{5})',
1236
- r'(\d{2}.*\sStreets)',
1237
- r'([A-Z].*\s\d{2},\sOmaha,\sNE\s\d{5})'
1238
- ]
1239
- addresses = []
1240
- for pattern in address_patterns:
1241
- addresses.extend(re.findall(pattern, response))
1242
- return addresses
1243
-
1244
- all_addresses = []
1245
-
1246
- def generate_map(location_names):
1247
- global all_addresses
1248
- all_addresses.extend(location_names)
1249
-
1250
- api_key = os.environ['GOOGLEMAPS_API_KEY']
1251
- gmaps = GoogleMapsClient(key=api_key)
1252
-
1253
- m = folium.Map(location=[41.2565, -95.9345], zoom_start=12)
1254
-
1255
- for location_name in all_addresses:
1256
- geocode_result = gmaps.geocode(location_name)
1257
- if geocode_result:
1258
- location = geocode_result[0]['geometry']['location']
1259
- folium.Marker(
1260
- [location['lat'], location['lng']],
1261
- tooltip=f"{geocode_result[0]['formatted_address']}"
1262
- ).add_to(m)
1263
-
1264
- map_html = m._repr_html_()
1265
- return map_html
1266
-
1267
  def fetch_local_news():
1268
  api_key = os.environ['SERP_API']
1269
  url = f'https://serpapi.com/search.json?engine=google_news&q=omaha headline&api_key={api_key}'
@@ -1338,44 +1308,29 @@ def fetch_local_news():
1338
  else:
1339
  return "<p>Failed to fetch local news</p>"
1340
 
1341
- # Voice Control
1342
- import numpy as np
1343
- import torch
1344
- from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
1345
-
1346
  model_id = 'openai/whisper-large-v3'
1347
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
1348
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
1349
  model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype,
1350
- #low_cpu_mem_usage=True,
1351
  use_safetensors=True).to(device)
1352
  processor = AutoProcessor.from_pretrained(model_id)
1353
-
1354
- # Optimized ASR pipeline
1355
  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)
1356
 
1357
  base_audio_drive = "/data/audio"
1358
 
1359
- import numpy as np
1360
-
1361
  def transcribe_function(stream, new_chunk):
1362
  try:
1363
  sr, y = new_chunk[0], new_chunk[1]
1364
  except TypeError:
1365
  print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
1366
  return stream, "", None
1367
-
1368
  y = y.astype(np.float32) / np.max(np.abs(y))
1369
-
1370
  if stream is not None:
1371
  stream = np.concatenate([stream, y])
1372
  else:
1373
  stream = y
1374
-
1375
  result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
1376
-
1377
  full_text = result.get("text", "")
1378
-
1379
  return stream, full_text, result
1380
 
1381
  def update_map_with_response(history):
@@ -1386,7 +1341,7 @@ def update_map_with_response(history):
1386
  return generate_map(addresses)
1387
 
1388
  def clear_textbox():
1389
- return ""
1390
 
1391
  def show_map_if_details(history, choice):
1392
  if choice in ["Details", "Conversational"]:
@@ -1396,7 +1351,7 @@ def show_map_if_details(history, choice):
1396
 
1397
  def generate_audio_elevenlabs(text):
1398
  XI_API_KEY = os.environ['ELEVENLABS_API']
1399
- VOICE_ID = 'd9MIrwLnvDeH7aZb61E9' # Replace with your voice ID
1400
  tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
1401
  headers = {
1402
  "Accept": "application/json",
@@ -1408,7 +1363,7 @@ def generate_audio_elevenlabs(text):
1408
  "voice_settings": {
1409
  "stability": 1.0,
1410
  "similarity_boost": 0.0,
1411
- "style": 0.60, # Adjust style for more romantic tone
1412
  "use_speaker_boost": False
1413
  }
1414
  }
@@ -1424,7 +1379,6 @@ def generate_audio_elevenlabs(text):
1424
  logging.error(f"Error generating audio: {response.text}")
1425
  return None
1426
 
1427
- # Stable Diffusion setup
1428
  pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
1429
  pipe = pipe.to("cuda")
1430
 
@@ -1437,9 +1391,8 @@ def generate_image(prompt):
1437
  ).images[0]
1438
  return image
1439
 
1440
- # Hardcoded prompt for image generation
1441
- 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"
1442
- 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."
1443
  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."
1444
 
1445
  def update_images():
@@ -1448,61 +1401,39 @@ def update_images():
1448
  image_3 = generate_image(hardcoded_prompt_3)
1449
  return image_1, image_2, image_3
1450
 
1451
- def show_login_screen():
1452
- return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
1453
-
1454
- def show_main_screen():
1455
- return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
1456
 
1457
- def login(username, password):
1458
- success, message = authenticate_user(username, password)
1459
- if success:
1460
- return show_main_screen()
 
1461
  else:
1462
- return gr.update(visible=True, value=message), gr.update(visible=False), gr.update(visible=False)
1463
 
1464
- def create_account(username, password):
1465
- success, message = register_user(username, password)
1466
- if success:
1467
- return gr.update(visible=True, value=message), gr.update(visible(False), gr.update(visible=False))
1468
- else:
1469
- return gr.update(visible(True, value=message), gr.update(visible=False), gr.update(visible(False)))
1470
-
1471
- with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
1472
- state = gr.State()
1473
-
1474
- # Login screen
1475
- with gr.Row(visible=True) as login_screen:
1476
  with gr.Column():
1477
- gr.Markdown("<h1>Login</h1>")
1478
- login_username = gr.Textbox(label="Username")
1479
- login_password = gr.Textbox(label="Password", type="password")
1480
  login_button = gr.Button("Login")
1481
- login_message = gr.Textbox(visible=False)
1482
- login_button.click(login, [login_username, login_password], [login_message, login_screen, state])
1483
- create_account_button = gr.Button("Create Account")
1484
- create_account_button.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [login_screen, create_account_screen])
1485
-
1486
- # Create account screen
1487
- with gr.Row(visible=False) as create_account_screen:
1488
  with gr.Column():
1489
- gr.Markdown("<h1>Create Account</h1>")
1490
- create_username = gr.Textbox(label="Username")
1491
- create_password = gr.Textbox(label="Password", type="password")
1492
  create_button = gr.Button("Create Account")
1493
- create_message = gr.Textbox(visible=False)
1494
- create_button.click(create_account, [create_username, create_password], [create_message, create_account_screen, state])
1495
- back_to_login_button = gr.Button("Back to Login")
1496
- back_to_login_button.click(lambda: (gr.update(visible=True), gr.update(visible(False))), None, [login_screen, create_account_screen])
1497
-
1498
- # Main application screen (initially hidden)
1499
- with gr.Row(visible=False) as main_screen:
1500
  with gr.Column():
1501
  state = gr.State()
1502
-
1503
  chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
1504
  choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
1505
-
1506
  gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
1507
  chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
1508
  chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
@@ -1511,26 +1442,21 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
1511
  chatbot.like(print_like_dislike, None, None)
1512
  clear_button = gr.Button("Clear")
1513
  clear_button.click(fn=clear_textbox, inputs=None, outputs=chat_input)
1514
-
1515
-
1516
  audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy')
1517
  audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="SAMLOne_real_time")
1518
-
1519
  gr.Markdown("<h1 style='color: red;'>Map</h1>", elem_id="location-markdown")
1520
  location_output = gr.HTML()
1521
  bot_msg.then(show_map_if_details, [chatbot, choice], [location_output, location_output])
1522
-
1523
  with gr.Column():
1524
  weather_output = gr.HTML(value=fetch_local_weather())
1525
  news_output = gr.HTML(value=fetch_local_news())
1526
- news_output = gr.HTML(value=fetch_local_events())
1527
-
1528
  with gr.Column():
1529
-
1530
  image_output_1 = gr.Image(value=generate_image(hardcoded_prompt_1), width=400, height=400)
1531
  image_output_2 = gr.Image(value=generate_image(hardcoded_prompt_2), width=400, height=400)
1532
  image_output_3 = gr.Image(value=generate_image(hardcoded_prompt_3), width=400, height=400)
1533
-
1534
  refresh_button = gr.Button("Refresh Images")
1535
  refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
1536
 
@@ -1538,3 +1464,12 @@ demo.queue()
1538
  demo.launch(share=True)
1539
 
1540
 
 
 
 
 
 
 
 
 
 
 
882
 
883
 
884
 
 
 
 
 
 
 
 
 
 
 
 
885
  import os
886
  import re
887
  import time
 
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
  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
  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
  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
  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>
 
1234
  }
1235
  return condition_map.get(condition, "c04d")
1236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1237
  def fetch_local_news():
1238
  api_key = os.environ['SERP_API']
1239
  url = f'https://serpapi.com/search.json?engine=google_news&q=omaha headline&api_key={api_key}'
 
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):
 
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"]:
 
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
  "voice_settings": {
1364
  "stability": 1.0,
1365
  "similarity_boost": 0.0,
1366
+ "style": 0.60,
1367
  "use_speaker_boost": False
1368
  }
1369
  }
 
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
  ).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
 
1398
  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
+ with gr.Row(visible=not authenticated) as login_row:
 
 
 
 
 
 
 
 
 
 
1416
  with gr.Column():
1417
+ gr.Markdown("### Login")
1418
+ username = gr.Textbox(label="Username")
1419
+ password = gr.Textbox(label="Password", type="password")
1420
  login_button = gr.Button("Login")
1421
+ login_output = gr.Markdown()
1422
+ login_button.click(fn=check_login, inputs=[username, password], outputs=[login_output, login_row, demo])
1423
+
 
 
 
 
1424
  with gr.Column():
1425
+ gr.Markdown("### Create Account")
1426
+ username_create = gr.Textbox(label="Username")
1427
+ password_create = gr.Textbox(label="Password", type="password")
1428
  create_button = gr.Button("Create Account")
1429
+ create_output = gr.Markdown()
1430
+ create_button.click(fn=create_account, inputs=[username_create, password_create], outputs=create_output)
1431
+
1432
+ with gr.Row(visible=authenticated) as main_interface:
 
 
 
1433
  with gr.Column():
1434
  state = gr.State()
 
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
  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
 
 
1464
  demo.launch(share=True)
1465
 
1466
 
1467
+
1468
+
1469
+
1470
+
1471
+
1472
+
1473
+
1474
+
1475
+