|
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."""
|
|
|
|
responder, links, titles, domains, published_dates = get_response(message, rerank_type="crossencoder")
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
response_parts = responder.split("References:")
|
|
main_response = response_parts[0].strip()
|
|
|
|
|
|
final_response = (
|
|
f"{main_response}\n\n"
|
|
f"References:\n"
|
|
f"{chr(10).join(hyperlinks)}"
|
|
)
|
|
|
|
return final_response
|
|
|
|
|
|
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() |