|
|
import os |
|
|
import streamlit as st |
|
|
from groq import Groq |
|
|
|
|
|
|
|
|
GROQ_API_KEY = "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" |
|
|
os.environ["GROQ_API_KEY"] = GROQ_API_KEY |
|
|
|
|
|
|
|
|
client = Groq(api_key=GROQ_API_KEY) |
|
|
|
|
|
|
|
|
st.title("Subject-specific AI Chatbot") |
|
|
st.write("Hello! I'm your AI Study Assistant. You can ask me any questions related to your subjects, and I'll try to help.") |
|
|
|
|
|
|
|
|
if 'conversation_history' not in st.session_state: |
|
|
st.session_state.conversation_history = [] |
|
|
|
|
|
|
|
|
subjects = ["Chemistry", "Computer", "English", "Islamiat", "Mathematics", "Physics", "Urdu"] |
|
|
|
|
|
|
|
|
def generate_chatbot_response(user_message): |
|
|
|
|
|
related_subject = None |
|
|
for subject in subjects: |
|
|
if subject.lower() in user_message.lower(): |
|
|
related_subject = subject |
|
|
break |
|
|
|
|
|
|
|
|
if "kisne banaya" in user_message.lower() or "who created you" in user_message.lower(): |
|
|
return "Mujhe Abdel Basit ne banaya hai. π" |
|
|
|
|
|
if related_subject: |
|
|
prompt = f"You are a helpful AI chatbot for studying {related_subject}. The user is asking: {user_message}. Provide a detailed, helpful response related to {related_subject}." |
|
|
else: |
|
|
prompt = f"You are a helpful AI chatbot. The user is asking: {user_message}. If the question is not related to any of the specified subjects (Chemistry, Computer, English, Islamiat, Mathematics, Physics, Urdu), politely let them know." |
|
|
|
|
|
|
|
|
chat_completion = client.chat.completions.create( |
|
|
messages=[{"role": "user", "content": prompt}], |
|
|
model="llama3-8b-8192", |
|
|
) |
|
|
|
|
|
response = chat_completion.choices[0].message.content |
|
|
return response |
|
|
|
|
|
|
|
|
user_input = st.chat_input("Ask me a subject-related question:") |
|
|
|
|
|
|
|
|
if user_input: |
|
|
chatbot_response = generate_chatbot_response(user_input) |
|
|
|
|
|
|
|
|
st.session_state.conversation_history.append(("User: " + user_input, "Chatbot: " + chatbot_response)) |
|
|
|
|
|
|
|
|
for question, answer in st.session_state.conversation_history: |
|
|
st.write(f"**{question}**") |
|
|
st.write(answer) |
|
|
|