Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
-
import random
|
3 |
import time
|
4 |
import openai
|
5 |
|
6 |
from llama_index import StorageContext, load_index_from_storage
|
7 |
-
import os
|
8 |
|
9 |
import pandas as pd
|
10 |
|
11 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
12 |
-
passwd = os.getenv("PASSWD_SECRET")
|
13 |
-
|
14 |
df = pd.read_csv("original_huberman.csv")
|
15 |
|
16 |
storage_context = StorageContext.from_defaults(persist_dir="./storage")
|
17 |
# load index
|
18 |
-
index = load_index_from_storage(storage_context)
|
19 |
-
|
20 |
-
query_engine = index.as_query_engine(similarity_top_k=4)
|
21 |
|
22 |
def get_podcast_and_youtube(response):
|
23 |
podcasts = []
|
@@ -32,6 +24,15 @@ with gr.Blocks() as demo:
|
|
32 |
gr.Markdown("<h1><center>HuberChat</center></h1>")
|
33 |
gr.Markdown("<p align='center'><img src='https://yt3.googleusercontent.com/5ONImZvpa9_hYK12Xek2E2JLzRc732DWsZMX2F-AZ1cTutTQLBuAmcEtFwrCgypqJncl5HrV2w=s900-c-k-c0x00ffffff-no-rj' height='50' width='95'></p>")
|
34 |
gr.Markdown("<p align='center' style='font-size: 20px;'>Hi! I am Andrew HuberChat, a chatbot trained to answer neurobiology.</p>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
with gr.Row().style():
|
36 |
with gr.Column(scale=0.85):
|
37 |
msg = gr.Textbox(
|
@@ -44,7 +45,7 @@ with gr.Blocks() as demo:
|
|
44 |
gr.Examples(
|
45 |
examples=["What is love?",
|
46 |
"Why should I get sunlight exposure?",
|
47 |
-
"
|
48 |
],
|
49 |
inputs=msg
|
50 |
)
|
@@ -52,7 +53,11 @@ with gr.Blocks() as demo:
|
|
52 |
|
53 |
clear = gr.Button("Clear")
|
54 |
|
55 |
-
def respond(message, chat_history):
|
|
|
|
|
|
|
|
|
56 |
response = query_engine.query(message)
|
57 |
bot_message = response.response
|
58 |
for i, row in get_podcast_and_youtube(response).iterrows():
|
@@ -61,9 +66,9 @@ with gr.Blocks() as demo:
|
|
61 |
time.sleep(1)
|
62 |
return "", chat_history
|
63 |
|
64 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
65 |
-
btn2.click(respond, [msg, chatbot], [msg, chatbot])
|
66 |
clear.click(lambda: None, None, chatbot, queue=False)
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
-
demo.launch(
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import time
|
3 |
import openai
|
4 |
|
5 |
from llama_index import StorageContext, load_index_from_storage
|
|
|
6 |
|
7 |
import pandas as pd
|
8 |
|
|
|
|
|
|
|
9 |
df = pd.read_csv("original_huberman.csv")
|
10 |
|
11 |
storage_context = StorageContext.from_defaults(persist_dir="./storage")
|
12 |
# load index
|
|
|
|
|
|
|
13 |
|
14 |
def get_podcast_and_youtube(response):
|
15 |
podcasts = []
|
|
|
24 |
gr.Markdown("<h1><center>HuberChat</center></h1>")
|
25 |
gr.Markdown("<p align='center'><img src='https://yt3.googleusercontent.com/5ONImZvpa9_hYK12Xek2E2JLzRc732DWsZMX2F-AZ1cTutTQLBuAmcEtFwrCgypqJncl5HrV2w=s900-c-k-c0x00ffffff-no-rj' height='50' width='95'></p>")
|
26 |
gr.Markdown("<p align='center' style='font-size: 20px;'>Hi! I am Andrew HuberChat, a chatbot trained to answer neurobiology.</p>")
|
27 |
+
gr.Markdown("<p align='center' style='font-size: 20px;'>Disclaimer: this is a fan-made project to highlight the work of Andrew Huberman. To support this project, please have a look at <a href='https://hubermanlab.com/'>Huberman Lab</a>.</p>")
|
28 |
+
with gr.Row().style():
|
29 |
+
with gr.Column(scale=1.0):
|
30 |
+
openai_api_key = gr.Textbox(
|
31 |
+
show_label=False,
|
32 |
+
placeholder="Set your OpenAI API key here.",
|
33 |
+
lines=1,
|
34 |
+
type="password"
|
35 |
+
).style(container=False)
|
36 |
with gr.Row().style():
|
37 |
with gr.Column(scale=0.85):
|
38 |
msg = gr.Textbox(
|
|
|
45 |
gr.Examples(
|
46 |
examples=["What is love?",
|
47 |
"Why should I get sunlight exposure?",
|
48 |
+
"What are the benefits of walks after lunch?"
|
49 |
],
|
50 |
inputs=msg
|
51 |
)
|
|
|
53 |
|
54 |
clear = gr.Button("Clear")
|
55 |
|
56 |
+
def respond(openai_api_key, message, chat_history):
|
57 |
+
openai.api_key = openai_api_key
|
58 |
+
index = load_index_from_storage(storage_context)
|
59 |
+
query_engine = index.as_query_engine(similarity_top_k=3)
|
60 |
+
|
61 |
response = query_engine.query(message)
|
62 |
bot_message = response.response
|
63 |
for i, row in get_podcast_and_youtube(response).iterrows():
|
|
|
66 |
time.sleep(1)
|
67 |
return "", chat_history
|
68 |
|
69 |
+
msg.submit(respond, [openai_api_key, msg, chatbot], [msg, chatbot])
|
70 |
+
btn2.click(respond, [openai_api_key, msg, chatbot], [msg, chatbot])
|
71 |
clear.click(lambda: None, None, chatbot, queue=False)
|
72 |
|
73 |
if __name__ == "__main__":
|
74 |
+
demo.launch()
|