Update app.py
Browse files
app.py
CHANGED
@@ -4,36 +4,112 @@ from all_rag_fns import do_rag
|
|
4 |
import gradio as gr
|
5 |
|
6 |
|
7 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
oai_api_key = os.getenv("OPENAI_API_KEY")
|
9 |
-
response,
|
10 |
user_input,
|
11 |
stream=False,
|
12 |
n_results=3,
|
13 |
-
model_name="gpt-4o-mini",
|
14 |
oai_api_key=oai_api_key,
|
|
|
15 |
)
|
16 |
-
|
|
|
|
|
|
|
17 |
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
with gr.
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
fn=gr_ch_if,
|
22 |
-
|
|
|
23 |
)
|
24 |
|
25 |
# Add the static markdown at the bottom
|
26 |
-
gr.Markdown(
|
27 |
-
"""This Gradio app was created for Alan Feder's [talk at the 2024 R/Gov Conference](https://rstats.ai/gov.html). \n\n The Github repository that houses all the code is [here](https://github.com/AlanFeder/rgov-2024) -- feel free to fork it and use it on your own!"""
|
28 |
)
|
29 |
gr.Markdown("***")
|
30 |
gr.Markdown("### Contact me!")
|
31 |
-
gr.Image("https://
|
32 |
gr.Markdown(
|
33 |
-
"""
|
|
|
|
|
34 |
)
|
35 |
|
|
|
|
|
36 |
if __name__ == "__main__":
|
37 |
-
|
38 |
-
|
|
|
39 |
)
|
|
|
4 |
import gradio as gr
|
5 |
|
6 |
|
7 |
+
def create_video_html(video_info: list) -> str:
|
8 |
+
html = """
|
9 |
+
<style>
|
10 |
+
.video-container {
|
11 |
+
display: flex;
|
12 |
+
flex-wrap: wrap;
|
13 |
+
justify-content: space-around;
|
14 |
+
}
|
15 |
+
.video-item {
|
16 |
+
width: 30%;
|
17 |
+
min-width: 300px;
|
18 |
+
margin-bottom: 20px;
|
19 |
+
}
|
20 |
+
@media (max-width: 1200px) {
|
21 |
+
.video-item {
|
22 |
+
width: 45%;
|
23 |
+
}
|
24 |
+
}
|
25 |
+
@media (max-width: 768px) {
|
26 |
+
.video-item {
|
27 |
+
width: 100%;
|
28 |
+
}
|
29 |
+
}
|
30 |
+
</style>
|
31 |
+
<div class="video-container">
|
32 |
+
"""
|
33 |
+
|
34 |
+
for vid_info in video_info:
|
35 |
+
yt_id = vid_info["VideoURL"].split("/")[-1].split("=")[-1]
|
36 |
+
yt_url = f"https://www.youtube.com/embed/{yt_id}"
|
37 |
+
html += f"""
|
38 |
+
<div class="video-item">
|
39 |
+
<h3>{vid_info["Title"]}</h3>
|
40 |
+
<p><em>{vid_info["Speaker"]}</em></p>
|
41 |
+
<p>Year: {vid_info["Year"]}</p>
|
42 |
+
<p>Similarity Score: {100 * vid_info["score"]:.0f}/100</p>
|
43 |
+
<iframe width="100%" height="215" src="{yt_url}" frameborder="0" allowfullscreen></iframe>
|
44 |
+
<details>
|
45 |
+
<summary>Transcript</summary>
|
46 |
+
<p>{vid_info["transcript"]}</p>
|
47 |
+
</details>
|
48 |
+
</div>
|
49 |
+
"""
|
50 |
+
html += "</div>"
|
51 |
+
return html
|
52 |
+
|
53 |
+
|
54 |
+
def gr_ch_if(user_input: str, model_radio: str):
|
55 |
+
model_name = "gpt-4o-mini" if model_radio == "Cheaper" else "gpt-4o"
|
56 |
oai_api_key = os.getenv("OPENAI_API_KEY")
|
57 |
+
response, retrieved_docs = do_rag(
|
58 |
user_input,
|
59 |
stream=False,
|
60 |
n_results=3,
|
|
|
61 |
oai_api_key=oai_api_key,
|
62 |
+
model_name=model_name,
|
63 |
)
|
64 |
+
video_html = create_video_html(retrieved_docs)
|
65 |
+
|
66 |
+
return response, video_html
|
67 |
+
|
68 |
|
69 |
+
# Create Gradio interface with single column layout
|
70 |
+
with gr.Blocks() as iface:
|
71 |
+
gr.Markdown("# RAG on R/Gov Talks")
|
72 |
+
gr.Markdown("Use Gradio to Run RAG on the previous R/Gov Talks")
|
73 |
|
74 |
+
with gr.Row():
|
75 |
+
with gr.Column(scale=1):
|
76 |
+
model_radio = gr.Radio(
|
77 |
+
choices=["Cheaper", "More Accurate"],
|
78 |
+
value=0,
|
79 |
+
label="Model",
|
80 |
+
info="Choose the model to use",
|
81 |
+
type="value",
|
82 |
+
interactive=True,
|
83 |
+
)
|
84 |
+
|
85 |
+
with gr.Column(scale=3):
|
86 |
+
query_input = gr.Textbox(label="Enter your question:")
|
87 |
+
|
88 |
+
response_output = gr.Textbox(label="Response", interactive=False)
|
89 |
+
video_output = gr.HTML(label="Relevant Videos")
|
90 |
+
|
91 |
+
query_input.submit(
|
92 |
fn=gr_ch_if,
|
93 |
+
inputs=[query_input, model_radio],
|
94 |
+
outputs=[response_output, video_output],
|
95 |
)
|
96 |
|
97 |
# Add the static markdown at the bottom
|
98 |
+
gr.Markdown("""This Gradio app was created for Alan Feder's [talk at the 2024 R/Gov Conference](https://rstats.ai/gov.html). \n\n The Github repository that houses all the code is [here](https://github.com/AlanFeder/rgov-2024) -- feel free to fork it and use it on your own!"""
|
|
|
99 |
)
|
100 |
gr.Markdown("***")
|
101 |
gr.Markdown("### Contact me!")
|
102 |
+
gr.Image("https://raw.githubusercontent.com/AlanFeder/rgov-2024/refs/heads/main/AJF_Headshot.jpg", width=60)
|
103 |
gr.Markdown(
|
104 |
+
"""
|
105 |
+
[Email](mailto:AlanFeder@gmail.com) | [Website](https://www.alanfeder.com/) | [LinkedIn](https://www.linkedin.com/in/alanfeder/) | [GitHub](https://github.com/AlanFeder)
|
106 |
+
"""
|
107 |
)
|
108 |
|
109 |
+
|
110 |
+
# Launch the app
|
111 |
if __name__ == "__main__":
|
112 |
+
iface.launch(
|
113 |
+
share=True,
|
114 |
+
favicon_path="favicon_io/favicon.ico",
|
115 |
)
|