Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from groq import Groq
|
|
|
4 |
|
5 |
# Set up the Groq API Key
|
6 |
GROQ_API_KEY = "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"
|
@@ -9,49 +10,22 @@ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
|
9 |
# Initialize the Groq client
|
10 |
client = Groq(api_key=GROQ_API_KEY)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Streamlit user interface setup
|
13 |
st.set_page_config(page_title="AI Study Assistant", page_icon="π€", layout="wide")
|
14 |
st.title("π Subject-specific AI Chatbot")
|
15 |
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.")
|
16 |
|
17 |
-
# Add sidebar with
|
18 |
st.sidebar.header("βοΈ Settings")
|
19 |
st.sidebar.write("Customize your chatbot experience!")
|
20 |
-
|
21 |
-
|
22 |
-
# Apply theme
|
23 |
-
if chat_theme == "Dark":
|
24 |
-
st.markdown("""
|
25 |
-
<style>
|
26 |
-
body {background-color: #1e1e1e; color: white;}
|
27 |
-
.stButton>button {background-color: #4CAF50; color: white;}
|
28 |
-
.chat-bubble {background-color: #2c2c2c; border-radius: 10px; padding: 10px;}
|
29 |
-
</style>
|
30 |
-
""", unsafe_allow_html=True)
|
31 |
-
elif chat_theme == "Blue":
|
32 |
-
st.markdown("""
|
33 |
-
<style>
|
34 |
-
body {background-color: #e3f2fd; color: black;}
|
35 |
-
.stButton>button {background-color: #2196F3; color: white;}
|
36 |
-
.chat-bubble {background-color: #bbdefb; border-radius: 10px; padding: 10px;}
|
37 |
-
</style>
|
38 |
-
""", unsafe_allow_html=True)
|
39 |
-
elif chat_theme == "Green":
|
40 |
-
st.markdown("""
|
41 |
-
<style>
|
42 |
-
body {background-color: #e8f5e9; color: black;}
|
43 |
-
.stButton>button {background-color: #4CAF50; color: white;}
|
44 |
-
.chat-bubble {background-color: #c8e6c9; border-radius: 10px; padding: 10px;}
|
45 |
-
</style>
|
46 |
-
""", unsafe_allow_html=True)
|
47 |
-
else:
|
48 |
-
st.markdown("""
|
49 |
-
<style>
|
50 |
-
body {background-color: #ffffff; color: black;}
|
51 |
-
.stButton>button {background-color: #008CBA; color: white;}
|
52 |
-
.chat-bubble {background-color: #f1f1f1; border-radius: 10px; padding: 10px;}
|
53 |
-
</style>
|
54 |
-
""", unsafe_allow_html=True)
|
55 |
|
56 |
# Initialize session state for maintaining conversation
|
57 |
if 'conversation_history' not in st.session_state:
|
@@ -60,42 +34,29 @@ if 'conversation_history' not in st.session_state:
|
|
60 |
# Define a list of subjects for which the chatbot will answer
|
61 |
subjects = ["Chemistry", "Computer", "English", "Islamiat", "Mathematics", "Physics", "Urdu"]
|
62 |
|
63 |
-
# Function to generate chatbot response based on subject-specific user input
|
64 |
def generate_chatbot_response(user_message):
|
65 |
-
|
66 |
-
related_subject = None
|
67 |
-
for subject in subjects:
|
68 |
-
if subject.lower() in user_message.lower():
|
69 |
-
related_subject = subject
|
70 |
-
break
|
71 |
|
72 |
-
# Custom response for "who created you" type of questions
|
73 |
if "kisne banaya" in user_message.lower() or "who created you" in user_message.lower():
|
74 |
-
return "I
|
75 |
|
76 |
-
if related_subject:
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
else:
|
79 |
-
|
80 |
|
81 |
-
|
82 |
-
chat_completion = client.chat.completions.create(
|
83 |
-
messages=[{"role": "user", "content": prompt}],
|
84 |
-
model="llama3-8b-8192", # You can replace with the appropriate model name
|
85 |
-
)
|
86 |
-
|
87 |
-
response = chat_completion.choices[0].message.content
|
88 |
-
return response
|
89 |
-
|
90 |
-
# User input for conversation (now placed at the bottom)
|
91 |
st.markdown("### π¬ Chat with me")
|
92 |
user_input = st.chat_input("Ask me a subject-related question:")
|
93 |
|
94 |
-
# Handle user input and display conversation
|
95 |
if user_input:
|
96 |
chatbot_response = generate_chatbot_response(user_input)
|
97 |
-
|
98 |
-
# Save the conversation history
|
99 |
st.session_state.conversation_history.append(("User: " + user_input, "Chatbot: " + chatbot_response))
|
100 |
|
101 |
# Display chat history
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from groq import Groq
|
4 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
5 |
|
6 |
# Set up the Groq API Key
|
7 |
GROQ_API_KEY = "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"
|
|
|
10 |
# Initialize the Groq client
|
11 |
client = Groq(api_key=GROQ_API_KEY)
|
12 |
|
13 |
+
# Initialize Hugging Face DeepSeek R1 model
|
14 |
+
pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-R1", trust_remote_code=True)
|
15 |
+
|
16 |
+
def generate_response_hf(user_message):
|
17 |
+
response = pipe(user_message, max_length=200, do_sample=True)
|
18 |
+
return response[0]['generated_text']
|
19 |
+
|
20 |
# Streamlit user interface setup
|
21 |
st.set_page_config(page_title="AI Study Assistant", page_icon="π€", layout="wide")
|
22 |
st.title("π Subject-specific AI Chatbot")
|
23 |
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.")
|
24 |
|
25 |
+
# Add sidebar with settings
|
26 |
st.sidebar.header("βοΈ Settings")
|
27 |
st.sidebar.write("Customize your chatbot experience!")
|
28 |
+
chat_model = st.sidebar.radio("Choose AI Model:", ["Groq API", "DeepSeek R1 (Hugging Face)"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Initialize session state for maintaining conversation
|
31 |
if 'conversation_history' not in st.session_state:
|
|
|
34 |
# Define a list of subjects for which the chatbot will answer
|
35 |
subjects = ["Chemistry", "Computer", "English", "Islamiat", "Mathematics", "Physics", "Urdu"]
|
36 |
|
|
|
37 |
def generate_chatbot_response(user_message):
|
38 |
+
related_subject = next((subject for subject in subjects if subject.lower() in user_message.lower()), None)
|
|
|
|
|
|
|
|
|
|
|
39 |
|
|
|
40 |
if "kisne banaya" in user_message.lower() or "who created you" in user_message.lower():
|
41 |
+
return "I was created by Abdul Basit π"
|
42 |
|
43 |
+
prompt = f"You are a helpful AI chatbot for studying {related_subject if related_subject else 'general knowledge'}. The user is asking: {user_message}. Provide a detailed, helpful response."
|
44 |
+
|
45 |
+
if chat_model == "Groq API":
|
46 |
+
chat_completion = client.chat.completions.create(
|
47 |
+
messages=[{"role": "user", "content": prompt}],
|
48 |
+
model="deepseek-chat"
|
49 |
+
)
|
50 |
+
return chat_completion.choices[0].message.content
|
51 |
else:
|
52 |
+
return generate_response_hf(prompt)
|
53 |
|
54 |
+
# User input for conversation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
st.markdown("### π¬ Chat with me")
|
56 |
user_input = st.chat_input("Ask me a subject-related question:")
|
57 |
|
|
|
58 |
if user_input:
|
59 |
chatbot_response = generate_chatbot_response(user_input)
|
|
|
|
|
60 |
st.session_state.conversation_history.append(("User: " + user_input, "Chatbot: " + chatbot_response))
|
61 |
|
62 |
# Display chat history
|