File size: 4,488 Bytes
f630b9e
 
 
83687f1
f630b9e
5a6df5d
f630b9e
 
 
 
 
5a6df5d
 
bdc7837
8b29d63
f630b9e
5a6df5d
990ca55
5a6df5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f792ef
 
f630b9e
5a6df5d
8b29d63
 
5a6df5d
ca57036
5a6df5d
 
 
 
 
 
7f792ef
5a6df5d
b77d772
5a6df5d
b77d772
5a6df5d
 
8b29d63
5a6df5d
8b29d63
5a6df5d
 
 
 
 
 
 
 
 
 
bdc7837
20c45d0
7f792ef
5a6df5d
ca57036
 
5a6df5d
 
7f792ef
 
 
bdc7837
 
7f792ef
990ca55
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import streamlit as st
from groq import Groq

# Set up the Groq API Key
GROQ_API_KEY = "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"
os.environ["GROQ_API_KEY"] = GROQ_API_KEY

# Initialize the Groq client
client = Groq(api_key=GROQ_API_KEY)

# Streamlit user interface setup
st.set_page_config(page_title="AI Study Assistant", page_icon="πŸ€–", layout="wide")
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.")

# Add sidebar with styling options
st.sidebar.header("βš™οΈ Settings")
st.sidebar.write("Customize your chatbot experience!")
chat_theme = st.sidebar.radio("Choose a theme:", ["Light", "Dark", "Blue", "Green"])

# Apply theme
if chat_theme == "Dark":
    st.markdown("""
        <style>
            body {background-color: #1e1e1e; color: white;}
            .stButton>button {background-color: #4CAF50; color: white;}
            .chat-bubble {background-color: #2c2c2c; border-radius: 10px; padding: 10px;}
        </style>
    """, unsafe_allow_html=True)
elif chat_theme == "Blue":
    st.markdown("""
        <style>
            body {background-color: #e3f2fd; color: black;}
            .stButton>button {background-color: #2196F3; color: white;}
            .chat-bubble {background-color: #bbdefb; border-radius: 10px; padding: 10px;}
        </style>
    """, unsafe_allow_html=True)
elif chat_theme == "Green":
    st.markdown("""
        <style>
            body {background-color: #e8f5e9; color: black;}
            .stButton>button {background-color: #4CAF50; color: white;}
            .chat-bubble {background-color: #c8e6c9; border-radius: 10px; padding: 10px;}
        </style>
    """, unsafe_allow_html=True)
else:
    st.markdown("""
        <style>
            body {background-color: #ffffff; color: black;}
            .stButton>button {background-color: #008CBA; color: white;}
            .chat-bubble {background-color: #f1f1f1; border-radius: 10px; padding: 10px;}
        </style>
    """, unsafe_allow_html=True)

# Initialize session state for maintaining conversation
if 'conversation_history' not in st.session_state:
    st.session_state.conversation_history = []

# Define a list of subjects for which the chatbot will answer
subjects = ["Chemistry", "Computer", "English", "Islamiat", "Mathematics", "Physics", "Urdu"]

# Function to generate chatbot response based on subject-specific user input
def generate_chatbot_response(user_message):
    # Check if the user's question is related to any subject
    related_subject = None
    for subject in subjects:
        if subject.lower() in user_message.lower():
            related_subject = subject
            break
    
    # Custom response for "who created you" type of questions
    if "kisne banaya" in user_message.lower() or "who created you" in user_message.lower():
        return "I Created by Abdul Basit 😊"

    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."

    # Generate response using Groq API
    chat_completion = client.chat.completions.create(
        messages=[{"role": "user", "content": prompt}],
        model="llama3-8b-8192",  # You can replace with the appropriate model name
    )

    response = chat_completion.choices[0].message.content
    return response

# User input for conversation (now placed at the bottom)
st.markdown("### πŸ’¬ Chat with me")
user_input = st.chat_input("Ask me a subject-related question:")

# Handle user input and display conversation
if user_input:
    chatbot_response = generate_chatbot_response(user_input)

    # Save the conversation history
    st.session_state.conversation_history.append(("User: " + user_input, "Chatbot: " + chatbot_response))

# Display chat history
st.markdown("---")
st.markdown("### πŸ—¨οΈ Chat History")
for question, answer in st.session_state.conversation_history:
    st.write(f"<div class='chat-bubble'><b>{question}</b></div>", unsafe_allow_html=True)
    st.write(f"<div class='chat-bubble'>{answer}</div>", unsafe_allow_html=True)