vtiyyal1 commited on
Commit
99f9e29
1 Parent(s): 9ca13f0

Upload app.py

Browse files

fixed citation at the end

Files changed (1) hide show
  1. app.py +36 -21
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
- return f"<a href='{url}'>{title}</a>" + " (" + domain + ")"
13
-
14
 
15
  def predict(message, history):
16
- print("get_responses: ")
17
- # print(get_response(message, rerank_type="crossencoder"))
18
  responder, links, titles, domains = get_response(message, rerank_type="crossencoder")
19
- for i in range(len(links)):
20
- links[i] = create_hyperlink(links[i], titles[i], domains[i])
21
-
22
- out = responder + "\n" + "\n".join(links)
23
-
24
- return out
25
-
26
-
27
- gr.ChatInterface(predict,
28
- examples = [
29
- "How many Americans Smoke?",
30
- "What are some measures taken by the Indian Government to reduce the smoking population?",
31
- "Does smoking negatively affect my health?"
32
- ]
33
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()