Upload app.py
Browse filesfixed citation at the end
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import openai
|
3 |
import gradio as gr
|
4 |
from full_chain import get_response
|
@@ -7,27 +6,43 @@ import os
|
|
7 |
api_key = os.getenv("OPENAI_API_KEY")
|
8 |
client = openai.OpenAI(api_key=api_key)
|
9 |
|
10 |
-
|
11 |
def create_hyperlink(url, title, domain):
|
12 |
-
|
13 |
-
|
14 |
|
15 |
def predict(message, history):
|
16 |
-
|
17 |
-
#
|
18 |
responder, links, titles, domains = get_response(message, rerank_type="crossencoder")
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
from full_chain import get_response
|
|
|
6 |
api_key = os.getenv("OPENAI_API_KEY")
|
7 |
client = openai.OpenAI(api_key=api_key)
|
8 |
|
|
|
9 |
def create_hyperlink(url, title, domain):
|
10 |
+
"""Create HTML hyperlink with domain information."""
|
11 |
+
return f"<a href='{url}' target='_blank'>{title}</a> ({domain})"
|
12 |
|
13 |
def predict(message, history):
|
14 |
+
"""Process user message and return response with hyperlinked sources."""
|
15 |
+
# Get response and source information
|
16 |
responder, links, titles, domains = get_response(message, rerank_type="crossencoder")
|
17 |
+
|
18 |
+
# The responder already contains the formatted response with numbered citations
|
19 |
+
# We just need to add the hyperlinked references at the bottom
|
20 |
+
hyperlinks = []
|
21 |
+
for i, (link, title, domain) in enumerate(zip(links, titles, domains), 1):
|
22 |
+
hyperlink = f"[{i}] {create_hyperlink(link, title, domain)}"
|
23 |
+
hyperlinks.append(hyperlink)
|
24 |
+
|
25 |
+
# Split the responder to separate the response from its references
|
26 |
+
response_parts = responder.split("References:")
|
27 |
+
main_response = response_parts[0].strip()
|
28 |
+
|
29 |
+
# Combine the response with hyperlinked references
|
30 |
+
final_response = (
|
31 |
+
f"{main_response}\n\n"
|
32 |
+
f"References:\n"
|
33 |
+
f"{chr(10).join(hyperlinks)}"
|
34 |
+
)
|
35 |
+
|
36 |
+
return final_response
|
37 |
+
|
38 |
+
# Initialize and launch Gradio interface
|
39 |
+
gr.ChatInterface(
|
40 |
+
predict,
|
41 |
+
examples=[
|
42 |
+
"How many Americans Smoke?",
|
43 |
+
"What are some measures taken by the Indian Government to reduce the smoking population?",
|
44 |
+
"Does smoking negatively affect my health?"
|
45 |
+
],
|
46 |
+
title="Tobacco Information Assistant",
|
47 |
+
description="Ask questions about tobacco-related topics and get answers with reliable sources."
|
48 |
+
).launch()
|