File size: 3,122 Bytes
f848c2c
 
 
 
 
 
 
 
 
 
 
 
0b41ad7
f848c2c
 
 
 
 
 
 
 
 
 
 
 
 
 
cd132d3
 
 
 
 
 
 
 
 
f848c2c
 
 
 
c9f667e
f848c2c
 
 
 
 
 
 
cd132d3
f848c2c
 
 
 
 
 
 
cd132d3
4bc3b34
 
0b41ad7
e9c4839
 
 
 
 
 
 
 
 
f848c2c
cd132d3
 
f848c2c
 
 
cd132d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import gradio as gr
import time
import openai

from llama_index import StorageContext, load_index_from_storage

import pandas as pd

df = pd.read_csv("original_huberman.csv")

storage_context = StorageContext.from_defaults(persist_dir="./storage")
# load index
import os

def get_podcast_and_youtube(response):
    podcasts = []
    for node in response.source_nodes:
        podcast = node.node.extra_info["filename"].split("/")[-1].split(".")[0]
        podcasts.append(podcast)

    mask = df.podcast.apply(lambda x: x in podcasts)
    return df.loc[mask]

with gr.Blocks() as demo:
    gr.Markdown("<h1><center>HuberChat</center></h1>")
    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>")
    gr.Markdown("<p align='center' style='font-size: 20px;'>Hi! I am Andrew HuberChat, a chatbot trained to answer neurobiology.</p>")
    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>")
    with gr.Row().style():
        with gr.Column(scale=1.0):
            openai_api_key = gr.Textbox(
                show_label=False,
                placeholder="Set your OpenAI API key here.",
                lines=1,
                type="password"
            ).style(container=False)
    with gr.Row().style():
        with gr.Column(scale=0.85):
            msg = gr.Textbox(
                show_label=False,
                placeholder="Enter text and press enter.",
                lines=1,
            ).style(container=False)
        with gr.Column(scale=0.15, min_width=0):
            btn2 = gr.Button("Send").style(full_height=True)
    gr.Examples(
        examples=["What is love?",
                  "Why should I get sunlight exposure?",
                  "What are the benefits of walks after lunch?"
                ],
        inputs=msg
    )
    chatbot = gr.Chatbot().style(height=250)
    
    clear = gr.Button("Clear")

    def respond(openai_api_key, message, chat_history):
        if not openai_api_key:
            return "No OpenAI key provided, please provide one.", chat_history
        os.environ["OPENAI_API_KEY"] = openai_api_key
        index = load_index_from_storage(storage_context)
        query_engine = index.as_query_engine(similarity_top_k=3)
        response = query_engine.query(message)
        bot_message = response.response
        for i, row in get_podcast_and_youtube(response).iterrows():
            bot_message += f"\n\n\n Source: {row.podcast} \n\n Link: {row.youtube_id}"
        chat_history.append((message, bot_message))
        time.sleep(1)
        return "", chat_history

    msg.submit(respond, [openai_api_key, msg, chatbot], [msg, chatbot])
    btn2.click(respond, [openai_api_key, msg, chatbot], [msg, chatbot])
    clear.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    demo.launch()