Update app.py
Browse files
app.py
CHANGED
|
@@ -9,18 +9,18 @@ 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
|
| 13 |
-
st.title("AI
|
| 14 |
-
st.write("Hello! I'm your AI Study Assistant.
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
# Function to generate chatbot response based on user input
|
| 20 |
def generate_chatbot_response(user_message):
|
| 21 |
-
# Generate prompt for Groq based on user message
|
| 22 |
prompt = f"You are a helpful AI chatbot. The user is asking: {user_message}. Provide a detailed, helpful response."
|
| 23 |
-
|
| 24 |
# Generate response using Groq API
|
| 25 |
chat_completion = client.chat.completions.create(
|
| 26 |
messages=[{"role": "user", "content": prompt}],
|
|
@@ -30,10 +30,17 @@ def generate_chatbot_response(user_message):
|
|
| 30 |
response = chat_completion.choices[0].message.content
|
| 31 |
return response
|
| 32 |
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
| 34 |
if user_input:
|
| 35 |
chatbot_response = generate_chatbot_response(user_input)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Initialize the Groq client
|
| 10 |
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
|
| 12 |
+
# Streamlit user interface setup
|
| 13 |
+
st.title("AI Chatbot")
|
| 14 |
+
st.write("Hello! I'm your AI Study Assistant. Ask me anything, and I'll try to help.")
|
| 15 |
|
| 16 |
+
# Initialize session state for maintaining conversation
|
| 17 |
+
if 'conversation_history' not in st.session_state:
|
| 18 |
+
st.session_state.conversation_history = []
|
| 19 |
|
| 20 |
# Function to generate chatbot response based on user input
|
| 21 |
def generate_chatbot_response(user_message):
|
|
|
|
| 22 |
prompt = f"You are a helpful AI chatbot. The user is asking: {user_message}. Provide a detailed, helpful response."
|
| 23 |
+
|
| 24 |
# Generate response using Groq API
|
| 25 |
chat_completion = client.chat.completions.create(
|
| 26 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 30 |
response = chat_completion.choices[0].message.content
|
| 31 |
return response
|
| 32 |
|
| 33 |
+
# User input for conversation
|
| 34 |
+
user_input = st.text_input("Ask me anything:")
|
| 35 |
+
|
| 36 |
+
# Handle user input and display conversation
|
| 37 |
if user_input:
|
| 38 |
chatbot_response = generate_chatbot_response(user_input)
|
| 39 |
+
|
| 40 |
+
# Save the conversation history
|
| 41 |
+
st.session_state.conversation_history.append(("User: " + user_input, "Chatbot: " + chatbot_response))
|
| 42 |
+
|
| 43 |
+
# Display chat history
|
| 44 |
+
for question, answer in st.session_state.conversation_history:
|
| 45 |
+
st.markdown(f"**{question}**")
|
| 46 |
+
st.markdown(answer)
|