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"{title} ({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()