Spaces:
Running
Running
Update app.py
Browse filesAdded mode dropdown and Brave search mode
app.py
CHANGED
|
@@ -36,6 +36,7 @@ pwdList = pwds.split(',')
|
|
| 36 |
DEEPSEEK_KEY=os.getenv('DEEPSEEK_KEY')
|
| 37 |
GROQ_KEY=os.getenv('GROQ_KEY')
|
| 38 |
BRAVE_KEY=os.getenv('BRAVE_KEY')
|
|
|
|
| 39 |
|
| 40 |
site = os.getenv('SITE')
|
| 41 |
if site == 'local':
|
|
@@ -49,6 +50,7 @@ else:
|
|
| 49 |
stock_data_path = dataDir + 'Stocks.txt'
|
| 50 |
|
| 51 |
braveNewsEndpoint = "https://api.search.brave.com/res/v1/news/search"
|
|
|
|
| 52 |
|
| 53 |
speak_file = dataDir + "speek.wav"
|
| 54 |
|
|
@@ -70,6 +72,24 @@ class MathReasoning(BaseModel):
|
|
| 70 |
steps: list[Step]
|
| 71 |
final_answer: str
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def get_brave_news(query: str, interval: str = 'pd'):
|
| 74 |
url = f'{braveNewsEndpoint}?q={quote(query)}&count=20&extra_snippets=true&freshness={interval}'
|
| 75 |
response = requests.get(
|
|
@@ -78,7 +98,9 @@ def get_brave_news(query: str, interval: str = 'pd'):
|
|
| 78 |
"X-Subscription-Token": BRAVE_KEY
|
| 79 |
},
|
| 80 |
)
|
| 81 |
-
rv =
|
|
|
|
|
|
|
| 82 |
jdata = response.json()
|
| 83 |
for item in jdata['results']:
|
| 84 |
title = item['title']
|
|
@@ -99,7 +121,6 @@ def get_brave_news(query: str, interval: str = 'pd'):
|
|
| 99 |
age = 'unknown'
|
| 100 |
rv += f' (Item source: {host}, Item age: {age})'
|
| 101 |
rv += ' *item separator* '
|
| 102 |
-
|
| 103 |
return rv
|
| 104 |
|
| 105 |
def Client():
|
|
@@ -624,7 +645,7 @@ def new_conversation(user):
|
|
| 624 |
os.remove(fpath)
|
| 625 |
return [None, [], gr.Markdown(value='', label='Dialog', container=True),
|
| 626 |
gr.Image(visible=False, value=None), gr.Image(visible=False, value=None), '',
|
| 627 |
-
gr.LinePlot(visible=False), gr.Dropdown(value="None")]
|
| 628 |
|
| 629 |
def updatePassword(txt):
|
| 630 |
password = txt.lower().strip()
|
|
@@ -648,7 +669,8 @@ def updatePassword(txt):
|
|
| 648 |
# ref = len(txt[ref:loc]) + len(frag)
|
| 649 |
# return txt
|
| 650 |
|
| 651 |
-
def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_image_file='',
|
|
|
|
| 652 |
image_gen_model = 'gpt-4o-2024-08-06'
|
| 653 |
user_window = user_window.lower().strip()
|
| 654 |
isBoss = False
|
|
@@ -714,7 +736,7 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 714 |
using_groq = False
|
| 715 |
reasoning = False
|
| 716 |
prompt = prompt.strip()
|
| 717 |
-
|
| 718 |
finish_reason = 'ok'
|
| 719 |
if prompt.lower().startswith('dsr1 '):
|
| 720 |
deepseek = True
|
|
@@ -750,10 +772,16 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 750 |
prompt = prompt[6:]
|
| 751 |
if deepseek:
|
| 752 |
prompt = prompt + '. Do not use Latex for math expressions.'
|
| 753 |
-
if
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
gen_image = (uploaded_image_file != '')
|
| 758 |
if chatType in special_chat_types:
|
| 759 |
(reply, tokens_in, tokens_out, tokens) = solve(prompt, chatType)
|
|
@@ -1222,7 +1250,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 1222 |
speak_output = gr.Button(value="Speak Dialog", visible=True)
|
| 1223 |
submit_button = gr.Button(value="Submit Prompt/Question")
|
| 1224 |
with gr.Row():
|
| 1225 |
-
prompt_window = gr.Textbox(label = "Prompt or Question", scale=
|
|
|
|
| 1226 |
news_period = gr.Dropdown(choices=news_interval_choices, interactive=True,label='News Window',scale=1)
|
| 1227 |
gr.Markdown('### **Dialog:**')
|
| 1228 |
#output_window = gr.Text(container=True, label='Dialog')
|
|
@@ -1236,10 +1265,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 1236 |
# plot = gr.Plot(visible=False)
|
| 1237 |
plot = gr.LinePlot(test_plot_df(), x="month", y="value", visible=False, label="Portfolio Value History")
|
| 1238 |
submit_button.click(chat,
|
| 1239 |
-
inputs=[prompt_window, user_window, password, history, output_window, model,
|
|
|
|
| 1240 |
outputs=[history, output_window, prompt_window, model, uploaded_image_file, plot])
|
| 1241 |
clear_button.click(fn=new_conversation, inputs=user_window,
|
| 1242 |
-
outputs=[prompt_window, history, output_window, image_window, image_window2,
|
|
|
|
| 1243 |
audio_widget.stop_recording(fn=transcribe, inputs=[user_window, password, audio_widget],
|
| 1244 |
outputs=[prompt_window])
|
| 1245 |
audio_widget.pause_recording(fn=pause_message, outputs=[prompt_window])
|
|
|
|
| 36 |
DEEPSEEK_KEY=os.getenv('DEEPSEEK_KEY')
|
| 37 |
GROQ_KEY=os.getenv('GROQ_KEY')
|
| 38 |
BRAVE_KEY=os.getenv('BRAVE_KEY')
|
| 39 |
+
BRAVE_SEARCH_KEY=os.getenv('BRAVE_SEARCH_KEY')
|
| 40 |
|
| 41 |
site = os.getenv('SITE')
|
| 42 |
if site == 'local':
|
|
|
|
| 50 |
stock_data_path = dataDir + 'Stocks.txt'
|
| 51 |
|
| 52 |
braveNewsEndpoint = "https://api.search.brave.com/res/v1/news/search"
|
| 53 |
+
braveSearchEndpoint = "https://api.search.brave.com/res/v1/web/search"
|
| 54 |
|
| 55 |
speak_file = dataDir + "speek.wav"
|
| 56 |
|
|
|
|
| 72 |
steps: list[Step]
|
| 73 |
final_answer: str
|
| 74 |
|
| 75 |
+
def get_brave_search_results(query: str):
|
| 76 |
+
rv = ''
|
| 77 |
+
url = f'{braveSearchEndpoint}?q={quote(query)}&count=20'
|
| 78 |
+
response = requests.get(
|
| 79 |
+
url,
|
| 80 |
+
headers= {"Accept": "application/json",
|
| 81 |
+
"X-Subscription-Token": BRAVE_SEARCH_KEY
|
| 82 |
+
},
|
| 83 |
+
)
|
| 84 |
+
jdata = response.json()
|
| 85 |
+
web_results = jdata['web']['results']
|
| 86 |
+
for item in web_results:
|
| 87 |
+
title = item['title']
|
| 88 |
+
description = item['description']
|
| 89 |
+
rv += f'{title}: {description} --'
|
| 90 |
+
return rv
|
| 91 |
+
|
| 92 |
+
|
| 93 |
def get_brave_news(query: str, interval: str = 'pd'):
|
| 94 |
url = f'{braveNewsEndpoint}?q={quote(query)}&count=20&extra_snippets=true&freshness={interval}'
|
| 95 |
response = requests.get(
|
|
|
|
| 98 |
"X-Subscription-Token": BRAVE_KEY
|
| 99 |
},
|
| 100 |
)
|
| 101 |
+
rv ='''Following are list items delineated by *item separator*
|
| 102 |
+
At the end of each item is (item source, item age)
|
| 103 |
+
*item separator* '''
|
| 104 |
jdata = response.json()
|
| 105 |
for item in jdata['results']:
|
| 106 |
title = item['title']
|
|
|
|
| 121 |
age = 'unknown'
|
| 122 |
rv += f' (Item source: {host}, Item age: {age})'
|
| 123 |
rv += ' *item separator* '
|
|
|
|
| 124 |
return rv
|
| 125 |
|
| 126 |
def Client():
|
|
|
|
| 645 |
os.remove(fpath)
|
| 646 |
return [None, [], gr.Markdown(value='', label='Dialog', container=True),
|
| 647 |
gr.Image(visible=False, value=None), gr.Image(visible=False, value=None), '',
|
| 648 |
+
gr.LinePlot(visible=False), gr.Dropdown(value="None"), gr.Dropdown(value='Chat')]
|
| 649 |
|
| 650 |
def updatePassword(txt):
|
| 651 |
password = txt.lower().strip()
|
|
|
|
| 669 |
# ref = len(txt[ref:loc]) + len(frag)
|
| 670 |
# return txt
|
| 671 |
|
| 672 |
+
def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_image_file='',
|
| 673 |
+
plot=None, news_interval = 'none', mode = 'Chat'):
|
| 674 |
image_gen_model = 'gpt-4o-2024-08-06'
|
| 675 |
user_window = user_window.lower().strip()
|
| 676 |
isBoss = False
|
|
|
|
| 736 |
using_groq = False
|
| 737 |
reasoning = False
|
| 738 |
prompt = prompt.strip()
|
| 739 |
+
augmented_prompt = prompt
|
| 740 |
finish_reason = 'ok'
|
| 741 |
if prompt.lower().startswith('dsr1 '):
|
| 742 |
deepseek = True
|
|
|
|
| 772 |
prompt = prompt[6:]
|
| 773 |
if deepseek:
|
| 774 |
prompt = prompt + '. Do not use Latex for math expressions.'
|
| 775 |
+
if past == []:
|
| 776 |
+
if mode == 'News':
|
| 777 |
+
if news_interval != "None":
|
| 778 |
+
news = get_brave_news(prompt, news_interval)
|
| 779 |
+
augmented_prompt = f'{news}\n{prompt}\nGive highest priority to information just provided\n'
|
| 780 |
+
augmented_prompt += 'Mention item source and item age for each item used\n'
|
| 781 |
+
elif mode == 'Search':
|
| 782 |
+
news = get_brave_search_results(prompt)
|
| 783 |
+
augmented_prompt = f'{news}\n{prompt}\nGive highest priority to information just provided\n'
|
| 784 |
+
past.append({"role":"user", "content":augmented_prompt})
|
| 785 |
gen_image = (uploaded_image_file != '')
|
| 786 |
if chatType in special_chat_types:
|
| 787 |
(reply, tokens_in, tokens_out, tokens) = solve(prompt, chatType)
|
|
|
|
| 1250 |
speak_output = gr.Button(value="Speak Dialog", visible=True)
|
| 1251 |
submit_button = gr.Button(value="Submit Prompt/Question")
|
| 1252 |
with gr.Row():
|
| 1253 |
+
prompt_window = gr.Textbox(label = "Prompt or Question", scale=7)
|
| 1254 |
+
mode = gr.Dropdown(choices=['Chat', 'News', 'Search'], label='Mode', scale=1, interactive=True)
|
| 1255 |
news_period = gr.Dropdown(choices=news_interval_choices, interactive=True,label='News Window',scale=1)
|
| 1256 |
gr.Markdown('### **Dialog:**')
|
| 1257 |
#output_window = gr.Text(container=True, label='Dialog')
|
|
|
|
| 1265 |
# plot = gr.Plot(visible=False)
|
| 1266 |
plot = gr.LinePlot(test_plot_df(), x="month", y="value", visible=False, label="Portfolio Value History")
|
| 1267 |
submit_button.click(chat,
|
| 1268 |
+
inputs=[prompt_window, user_window, password, history, output_window, model,
|
| 1269 |
+
uploaded_image_file, plot, news_period, mode],
|
| 1270 |
outputs=[history, output_window, prompt_window, model, uploaded_image_file, plot])
|
| 1271 |
clear_button.click(fn=new_conversation, inputs=user_window,
|
| 1272 |
+
outputs=[prompt_window, history, output_window, image_window, image_window2,
|
| 1273 |
+
uploaded_image_file, plot, news_period, mode])
|
| 1274 |
audio_widget.stop_recording(fn=transcribe, inputs=[user_window, password, audio_widget],
|
| 1275 |
outputs=[prompt_window])
|
| 1276 |
audio_widget.pause_recording(fn=pause_message, outputs=[prompt_window])
|