syllchat / app.py
anithai's picture
app.py
a6576b4 verified
Raw History Blame Contribute Delete
2.41 kB
import gradio as gr
import pandas as pd
import requests
def load_keywords(file_path):
df = pd.read_csv(file_path)
keyword_subject_map = {}
for index, row in df.iterrows():
keyword = row['keyword'].strip().lower()
variations = row['variations'].split(';')
subject = row['subject'].strip()
# Store the keyword with its subject
keyword_subject_map[keyword] = subject
for variation in variations:
keyword_subject_map[variation.strip().lower()] = subject
return keyword_subject_map
# Get the matching subject based on the user's question
def get_matching_subject(question, keyword_map):
for keyword, subject in keyword_map.items():
if keyword in question:
return subject
return None
# Call the Gemini API
def fetch_response_from_gemini(prompt, api_key):
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={api_key}"
headers = {"Content-Type": "application/json"}
body = {
"contents": [{"parts": [{"text": prompt}]}]
}
response = requests.post(url, headers=headers, json=body)
if response.status_code == 200:
result = response.json()
candidates = result.get("candidates", [])
if candidates:
return candidates[0]["content"]["parts"][0]["text"]
else:
return "No response content available."
else:
return f"Error fetching response: {response.text}"
# Main chatbot function
def chatbot(question):
api_key = "AIzaSyANMb7mDPE0Ji9eV1hYC_Yzj8IpYoJW4LE" # Replace with your actual API key
keyword_map = load_keywords("sylchat_key.csv")
user_question = question.lower()
subject = get_matching_subject(user_question, keyword_map)
if subject is None:
return "This is not in the syllabus."
if subject not in user_question:
user_question += f" in {subject}"
return fetch_response_from_gemini(user_question, api_key)
# Gradio Interface
iface = gr.Interface(
fn=chatbot,
inputs=gr.Textbox(label="Ask a syllabus question", placeholder="Enter your question here..."),
outputs=gr.Textbox(label="Response"),
title="Syllabus-Based Chatbot",
description="💡 Ask syllabus-based questions from Java, RDBMS, Software Testing, or Cloud Computing."
)
if __name__ == "__main__":
iface.launch()