import gradio as gr import pandas as pd import requests import json # Load keywords from CSV into a dictionary def load_keywords(file_path): keywords_dict = {} df = pd.read_csv(file_path) for index, row in df.iterrows(): keywords = row.iloc[1].split(',') for keyword in keywords: keywords_dict[keyword.strip().lower()] = row.iloc[0].strip().lower() return keywords_dict # Load your CSV data (ensure the file path is correct) keywords_dict = load_keywords("java_chatbot_keywords.csv") # Adjust this path as necessary def fetch_response_from_gemini(prompt): api_key = "AIzaSyANMb7mDPE0Ji9eV1hYC_Yzj8IpYoJW4LE" # Replace with your actual API Key url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={api_key}" headers = { "Content-Type": "application/json" } json_body = { "contents": [ { "parts": [ {"text": prompt} ] } ] } response = requests.post(url, headers=headers, json=json_body) try: response_data = response.json() candidates = response_data.get("candidates", []) if candidates: content = candidates[0].get("content", {}) if content: parts = content.get("parts", []) if parts: return parts[0].get("text", "No text content available.") return "No content available in the parts array." return "No content available in the response." return "No candidates available in the response." except Exception as e: return f"Error fetching response: {str(e)}" def chatbot_interface(user_question): user_question = user_question.lower() for keyword in keywords_dict.keys(): if keyword in user_question: if "java" not in user_question: user_question += " java" return fetch_response_from_gemini(user_question) return "This is not in the syllabus." iface = gr.Interface( fn=chatbot_interface, inputs=gr.Textbox(label="Enter your question"), outputs=gr.Textbox(label="Response"), title="Syllabus Related Answer Chatbot", description="Ask syllabus-related questions and get accurate responses." ) iface.launch(share=True)