Pijush2023 commited on
Commit
011279d
·
verified ·
1 Parent(s): 5e2ba8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -131
app.py CHANGED
@@ -1,49 +1,59 @@
1
- import logging
2
-
3
- # Set up logging
4
- logging.basicConfig(level=logging.DEBUG)
5
- from langchain_openai import OpenAIEmbeddings
6
  import os
7
  import re
8
- import folium
9
- import gradio as gr
10
  import time
11
  import requests
12
- from googlemaps import Client as GoogleMapsClient
13
- from gtts import gTTS
 
14
  import tempfile
15
- import string
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
 
17
  embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
18
 
19
- from pinecone import Pinecone, ServerlessSpec
 
20
  pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
21
 
22
  index_name = "omaha-details"
23
-
24
- from langchain_pinecone import PineconeVectorStore
25
-
26
  vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)
27
  retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
28
 
29
- from langchain_openai import ChatOpenAI
30
- from langchain.prompts import PromptTemplate
31
- from langchain.chains import RetrievalQA
32
- from langchain.chains.conversation.memory import ConversationBufferWindowMemory
33
- from langchain.agents import Tool, initialize_agent
 
 
 
 
34
 
35
- # Build prompt
36
- template1 = """You are an expert concierge who is helpful and a renowned guide for Omaha, Nebraska.Based on todays weather is sunny bright day and date is 17th june 2024 , Use the following pieces of context,
37
- 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.
38
  Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and
39
  event type and description. Always say "It was my pleasure!" at the end of the answer.
40
  {context}
41
  Question: {question}
42
  Helpful Answer:"""
43
 
44
- template2 = """You are an expert concierge who is helpful and a renowned guide for Omaha, Nebraska.Based on todays weather is sunny bright day and date is 17th 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,
45
- 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.
46
- Keep the answer short and sweet crisp.Always say "It was my pleasure!" at the end of the answer.
47
  {context}
48
  Question: {question}
49
  Helpful Answer:"""
@@ -51,15 +61,6 @@ Helpful Answer:"""
51
  QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
52
  QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
53
 
54
- chat_model = ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'],
55
- temperature=0, model='gpt-4o')
56
-
57
- conversational_memory = ConversationBufferWindowMemory(
58
- memory_key='chat_history',
59
- k=10,
60
- return_messages=True
61
- )
62
-
63
  # Define the retrieval QA chain
64
  def build_qa_chain(prompt_template):
65
  qa_chain = RetrievalQA.from_chain_type(
@@ -72,7 +73,7 @@ def build_qa_chain(prompt_template):
72
  Tool(
73
  name='Knowledge Base',
74
  func=qa_chain,
75
- description='use this tool when answering general knowledge queries to get more information about the topic'
76
  )
77
  ]
78
  return qa_chain, tools
@@ -108,17 +109,6 @@ def generate_answer(message, choice):
108
  addresses = extract_addresses(response['output'])
109
  return response['output'], addresses
110
 
111
- # def bot(history, choice):
112
- # if not history:
113
- # return history
114
- # response = generate_answer(history[-1][0], choice)
115
- # history[-1][1] = ""
116
- # for character in response:
117
- # history[-1][1] += character
118
- # time.sleep(0.05)
119
- # yield history
120
-
121
- # Define the bot function
122
  def bot(history, choice):
123
  if not history:
124
  return history
@@ -130,6 +120,7 @@ def bot(history, choice):
130
  yield history
131
  if addresses:
132
  return history, addresses
 
133
  def add_message(history, message):
134
  history.append((message, None))
135
  return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
@@ -137,27 +128,25 @@ def add_message(history, message):
137
  def print_like_dislike(x: gr.LikeData):
138
  print(x.index, x.value, x.liked)
139
 
140
- # Function to extract addresses from the chatbot's response
141
  def extract_addresses(response):
142
  if not isinstance(response, str):
143
  response = str(response)
144
- address_pattern_1 = r'([A-Z].*,\sOmaha,\sNE\s\d{5})'
145
- address_pattern_2 = r'(\d{4}\s.*,\sOmaha,\sNE\s\d{5})'
146
- address_pattern_3 = r'([A-Z].*,\sNE\s\d{5})'
147
- address_pattern_4 = r'([A-Z].*,.*\sSt,\sOmaha,\sNE\s\d{5})'
148
- address_pattern_5 = r'([A-Z].*,.*\sStreets,\sOmaha,\sNE\s\d{5})'
149
- address_pattern_6 = r'(\d{2}.*\sStreets)'
150
- address_pattern_7 = r'([A-Z].*\s\d{2},\sOmaha,\sNE\s\d{5})'
151
- addresses = re.findall(address_pattern_1, response) + re.findall(address_pattern_2, response) + \
152
- re.findall(address_pattern_3, response) + re.findall(address_pattern_4, response) + \
153
- re.findall(address_pattern_5, response) + re.findall(address_pattern_6, response) + \
154
- re.findall(address_pattern_7, response)
 
155
  return addresses
156
 
157
- # Store all found addresses
158
  all_addresses = []
159
 
160
- # Map generation function using Google Maps Geocoding API
161
  def generate_map(location_names):
162
  global all_addresses
163
  all_addresses.extend(location_names)
@@ -179,7 +168,6 @@ def generate_map(location_names):
179
  map_html = m._repr_html_()
180
  return map_html
181
 
182
- # Function to fetch local news
183
  def fetch_local_news():
184
  api_key = os.environ['SERP_API']
185
  url = f'https://serpapi.com/search.json?engine=google_news&q=ohama headline&api_key={api_key}'
@@ -254,23 +242,7 @@ def fetch_local_news():
254
  return news_html
255
  else:
256
  return "<p>Failed to fetch local news</p>"
257
- # Function to get the current weather
258
- def get_current_weather():
259
- try:
260
- api_key = os.environ['WEATHER_API']
261
- url = f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/omaha?unitGroup=metric&include=events%2Calerts%2Chours%2Cdays%2Ccurrent&key={api_key}'
262
- response = requests.get(url)
263
- response.raise_for_status()
264
- jsonData = response.json()
265
- current_conditions = jsonData.get("currentConditions", {})
266
- temp = current_conditions.get("temp", "N/A")
267
- condition = current_conditions.get("conditions", "N/A")
268
- return f"{temp}°C and {condition}"
269
- except requests.exceptions.RequestException as e:
270
- logging.error(f"Failed to fetch local weather: {e}")
271
- return "N/A"
272
-
273
- # Function to fetch local events
274
  def fetch_local_events():
275
  api_key = os.environ['SERP_API']
276
  url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Omaha&hl=en&gl=us&api_key={api_key}'
@@ -348,9 +320,6 @@ def fetch_local_events():
348
  else:
349
  return "<p>Failed to fetch local events</p>"
350
 
351
-
352
-
353
- # Function to fetch local weather
354
  def fetch_local_weather():
355
  try:
356
  api_key = os.environ['WEATHER_API']
@@ -431,48 +400,6 @@ def get_weather_icon(condition):
431
  }
432
  return condition_map.get(condition, "c01d")
433
 
434
-
435
- # Voice Control
436
- import numpy as np
437
- import torch
438
- from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
439
-
440
- model_id = 'openai/whisper-large-v3'
441
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
442
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
443
- model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype,
444
- #low_cpu_mem_usage=True,
445
- use_safetensors=True).to(device)
446
- processor = AutoProcessor.from_pretrained(model_id)
447
-
448
- # Optimized ASR pipeline
449
- 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)
450
-
451
- base_audio_drive = "/data/audio"
452
-
453
- import numpy as np
454
-
455
- def transcribe_function(stream, new_chunk):
456
- try:
457
- sr, y = new_chunk[0], new_chunk[1]
458
- except TypeError:
459
- print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
460
- return stream, "", None
461
-
462
- y = y.astype(np.float32) / np.max(np.abs(y))
463
-
464
- if stream is not None:
465
- stream = np.concatenate([stream, y])
466
- else:
467
- stream = y
468
-
469
- result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
470
-
471
- full_text = result.get("text", "")
472
-
473
- return stream, full_text, result
474
-
475
- # Map Retrieval Function for location finder
476
  def update_map_with_response(history):
477
  if not history:
478
  return ""
@@ -487,8 +414,7 @@ def show_map_if_details(history,choice):
487
  if choice in ["Details", "Conversational"]:
488
  return gr.update(visible=True), update_map_with_response(history)
489
  else:
490
- return gr.update(visible=False), ""
491
- #return gr.update(visible(False), "")
492
 
493
  def generate_audio_elevenlabs(text):
494
  XI_API_KEY = os.environ['ELEVENLABS_API']
@@ -520,6 +446,22 @@ def generate_audio_elevenlabs(text):
520
  logging.error(f"Error generating audio: {response.text}")
521
  return None
522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
523
  # Gradio Blocks interface
524
  with gr.Blocks(theme='rawrsor1/Everforest') as demo:
525
  with gr.Row():
@@ -537,8 +479,6 @@ with gr.Blocks(theme='rawrsor1/Everforest') as demo:
537
  ''')
538
  chatbot = gr.Chatbot([], elem_id="chatbot", bubble_full_width=False)
539
 
540
-
541
-
542
  with gr.Column():
543
  weather_output = gr.HTML(value=fetch_local_weather())
544
 
@@ -580,17 +520,18 @@ with gr.Blocks(theme='rawrsor1/Everforest') as demo:
580
  gr.Markdown("<h1>Listen the audio</h1>", elem_id="audio-markdown")
581
 
582
  audio_output = gr.Audio()
583
- #bot_msg.then(generate_audio_elevenlabs, chatbot, audio_output)
584
  bot_msg_audio = bot_msg.then(lambda history: generate_audio_elevenlabs(history[-1][1]), chatbot, audio_output)
585
  with gr.Column():
586
  gr.Markdown("<h1>Local Events</h1>", elem_id="events-markdown")
587
 
588
  news_output = gr.HTML(value=fetch_local_events())
589
 
590
- setup_ui()
591
-
592
-
 
593
 
 
594
 
595
  demo.queue()
596
  demo.launch(share=True)
 
 
 
 
 
 
1
  import os
2
  import re
 
 
3
  import time
4
  import requests
5
+ import logging
6
+ import folium
7
+ import gradio as gr
8
  import tempfile
9
+ import numpy as np
10
+ from gtts import gTTS
11
+ from googlemaps import Client as GoogleMapsClient
12
+ from diffusers import StableDiffusion3Pipeline
13
+
14
+ from langchain_openai import OpenAIEmbeddings, ChatOpenAI
15
+ from langchain_pinecone import PineconeVectorStore
16
+ from langchain.prompts import PromptTemplate
17
+ from langchain.chains import RetrievalQA
18
+ from langchain.chains.conversation.memory import ConversationBufferWindowMemory
19
+ from langchain.agents import Tool, initialize_agent
20
+
21
+ # Set up logging
22
+ logging.basicConfig(level=logging.DEBUG)
23
 
24
+ # Initialize OpenAI embeddings
25
  embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
26
 
27
+ # Initialize Pinecone
28
+ from pinecone import Pinecone
29
  pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
30
 
31
  index_name = "omaha-details"
 
 
 
32
  vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)
33
  retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
34
 
35
+ # Initialize ChatOpenAI model
36
+ chat_model = ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'],
37
+ temperature=0, model='gpt-4o')
38
+
39
+ conversational_memory = ConversationBufferWindowMemory(
40
+ memory_key='chat_history',
41
+ k=10,
42
+ return_messages=True
43
+ )
44
 
45
+ # Define prompt templates
46
+ template1 = """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 the date is 17th June 2024, use the following pieces of context,
47
+ 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.
48
  Use fifteen sentences maximum. Keep the answer as detailed as possible. Always include the address, time, date, and
49
  event type and description. Always say "It was my pleasure!" at the end of the answer.
50
  {context}
51
  Question: {question}
52
  Helpful Answer:"""
53
 
54
+ 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 the date is 17th 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,
55
+ 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.
56
+ Keep the answer short and sweet and crisp. Always say "It was my pleasure!" at the end of the answer.
57
  {context}
58
  Question: {question}
59
  Helpful Answer:"""
 
61
  QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
62
  QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
63
 
 
 
 
 
 
 
 
 
 
64
  # Define the retrieval QA chain
65
  def build_qa_chain(prompt_template):
66
  qa_chain = RetrievalQA.from_chain_type(
 
73
  Tool(
74
  name='Knowledge Base',
75
  func=qa_chain,
76
+ description='Use this tool when answering general knowledge queries to get more information about the topic'
77
  )
78
  ]
79
  return qa_chain, tools
 
109
  addresses = extract_addresses(response['output'])
110
  return response['output'], addresses
111
 
 
 
 
 
 
 
 
 
 
 
 
112
  def bot(history, choice):
113
  if not history:
114
  return history
 
120
  yield history
121
  if addresses:
122
  return history, addresses
123
+
124
  def add_message(history, message):
125
  history.append((message, None))
126
  return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
 
128
  def print_like_dislike(x: gr.LikeData):
129
  print(x.index, x.value, x.liked)
130
 
 
131
  def extract_addresses(response):
132
  if not isinstance(response, str):
133
  response = str(response)
134
+ address_patterns = [
135
+ r'([A-Z].*,\sOmaha,\sNE\s\d{5})',
136
+ r'(\d{4}\s.*,\sOmaha,\sNE\s\d{5})',
137
+ r'([A-Z].*,\sNE\s\d{5})',
138
+ r'([A-Z].*,.*\sSt,\sOmaha,\sNE\s\d{5})',
139
+ r'([A-Z].*,.*\sStreets,\sOmaha,\sNE\s\d{5})',
140
+ r'(\d{2}.*\sStreets)',
141
+ r'([A-Z].*\s\d{2},\sOmaha,\sNE\s\d{5})'
142
+ ]
143
+ addresses = []
144
+ for pattern in address_patterns:
145
+ addresses.extend(re.findall(pattern, response))
146
  return addresses
147
 
 
148
  all_addresses = []
149
 
 
150
  def generate_map(location_names):
151
  global all_addresses
152
  all_addresses.extend(location_names)
 
168
  map_html = m._repr_html_()
169
  return map_html
170
 
 
171
  def fetch_local_news():
172
  api_key = os.environ['SERP_API']
173
  url = f'https://serpapi.com/search.json?engine=google_news&q=ohama headline&api_key={api_key}'
 
242
  return news_html
243
  else:
244
  return "<p>Failed to fetch local news</p>"
245
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  def fetch_local_events():
247
  api_key = os.environ['SERP_API']
248
  url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Omaha&hl=en&gl=us&api_key={api_key}'
 
320
  else:
321
  return "<p>Failed to fetch local events</p>"
322
 
 
 
 
323
  def fetch_local_weather():
324
  try:
325
  api_key = os.environ['WEATHER_API']
 
400
  }
401
  return condition_map.get(condition, "c01d")
402
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403
  def update_map_with_response(history):
404
  if not history:
405
  return ""
 
414
  if choice in ["Details", "Conversational"]:
415
  return gr.update(visible=True), update_map_with_response(history)
416
  else:
417
+ return gr.update(visible(False), "")
 
418
 
419
  def generate_audio_elevenlabs(text):
420
  XI_API_KEY = os.environ['ELEVENLABS_API']
 
446
  logging.error(f"Error generating audio: {response.text}")
447
  return None
448
 
449
+ # Stable Diffusion setup
450
+ pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
451
+ pipe = pipe.to("cuda")
452
+
453
+ def generate_image(prompt):
454
+ image = pipe(
455
+ prompt,
456
+ negative_prompt="",
457
+ num_inference_steps=28,
458
+ guidance_scale=7.0,
459
+ ).images[0]
460
+ return image
461
+
462
+ # Hardcoded prompt for image generation
463
+ hardcoded_prompt = "A cat holding a sign that says hello world"
464
+
465
  # Gradio Blocks interface
466
  with gr.Blocks(theme='rawrsor1/Everforest') as demo:
467
  with gr.Row():
 
479
  ''')
480
  chatbot = gr.Chatbot([], elem_id="chatbot", bubble_full_width=False)
481
 
 
 
482
  with gr.Column():
483
  weather_output = gr.HTML(value=fetch_local_weather())
484
 
 
520
  gr.Markdown("<h1>Listen the audio</h1>", elem_id="audio-markdown")
521
 
522
  audio_output = gr.Audio()
 
523
  bot_msg_audio = bot_msg.then(lambda history: generate_audio_elevenlabs(history[-1][1]), chatbot, audio_output)
524
  with gr.Column():
525
  gr.Markdown("<h1>Local Events</h1>", elem_id="events-markdown")
526
 
527
  news_output = gr.HTML(value=fetch_local_events())
528
 
529
+ with gr.Column():
530
+ gr.Markdown("<h1>Generated Image</h1>", elem_id="image-markdown")
531
+
532
+ image_output = gr.Image(value=generate_image(hardcoded_prompt))
533
 
534
+ setup_ui()
535
 
536
  demo.queue()
537
  demo.launch(share=True)