File size: 1,864 Bytes
4c7c1f7 |
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 |
import openai
import gradio as gr
from full_chain import get_response
import os
api_key = os.getenv("OPENAI_API_KEY")
client = openai.OpenAI(api_key=api_key)
def create_hyperlink(url, title, domain):
"""Create HTML hyperlink with domain information."""
return f"<a href='{url}' target='_blank'>{title}</a> ({domain})"
def predict(message, history):
"""Process user message and return response with hyperlinked sources."""
# Get response and source information
responder, links, titles, domains, published_dates = get_response(message, rerank_type="crossencoder")
# The responder already contains the formatted response with numbered citations
# We just need to add the hyperlinked references at the bottom
hyperlinks = []
for i, (link, title, domain, published_date) in enumerate(zip(links, titles, domains, published_dates), 1):
hyperlink = f"[{i}] {create_hyperlink(link, title, domain)} {published_date}"
hyperlinks.append(hyperlink)
# Split the responder to separate the response from its references
response_parts = responder.split("References:")
main_response = response_parts[0].strip()
# Combine the response with hyperlinked references
final_response = (
f"{main_response}\n\n"
f"References:\n"
f"{chr(10).join(hyperlinks)}"
)
return final_response
# Initialize and launch Gradio interface
gr.ChatInterface(
predict,
examples=[
"How many Americans Smoke?",
"What are some measures taken by the Indian Government to reduce the smoking population?",
"Does smoking negatively affect my health?"
],
title="Tobacco Information Assistant",
description="Ask questions about tobacco-related topics and get answers with reliable sources."
).launch() |