Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| # Set up the Groq API endpoint and your API key | |
| GROQ_API_URL = "https://api.groq.com/v1/chat/completions" | |
| GROQ_API_KEY = "your_groq_api_key_here" # Replace with your actual Groq API key | |
| # Streamlit app title | |
| st.title("Educational Chatbot") | |
| # Initialize session state for chat history | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display chat messages from history on app rerun | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| # Accept user input | |
| if prompt := st.chat_input("What is your question?"): | |
| # Add user message to chat history | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| # Display user message in chat message container | |
| with st.chat_message("user"): | |
| st.markdown(prompt) | |
| # Send user message to Groq API | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "model": "groq-3.5-turbo", # Replace with the model you want to use | |
| "messages": st.session_state.messages | |
| } | |
| response = requests.post(GROQ_API_URL, headers=headers, json=data) | |
| if response.status_code == 200: | |
| chatbot_response = response.json()["choices"][0]["message"]["content"] | |
| # Add chatbot response to chat history | |
| st.session_state.messages.append({"role": "assistant", "content": chatbot_response}) | |
| # Display chatbot response in chat message container | |
| with st.chat_message("assistant"): | |
| st.markdown(chatbot_response) | |
| else: | |
| st.error("Failed to get response from Groq API") |