Spaces:
Sleeping
Sleeping
import streamlit as st | |
import requests | |
import os | |
# Retrieve API key from environment variables | |
api_key = st.secrets["groq_api_key"] | |
# Function to interact with Groq API | |
def get_groq_response(prompt): | |
url = "https://api.groq.com/inference" # Replace with the actual Groq API endpoint | |
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"} | |
data = {"input": prompt} | |
response = requests.post(url, headers=headers, json=data) | |
if response.status_code == 200: | |
return response.json().get("output", "No response") | |
else: | |
return f"Error: {response.status_code}, {response.text}" | |
# Streamlit app code | |
st.title("Groq API Chatbot") | |
if "conversation" not in st.session_state: | |
st.session_state.conversation = [] | |
user_input = st.text_input("You: ", "") | |
if st.button("Send"): | |
if user_input: | |
st.session_state.conversation.append(f"You: {user_input}") | |
# Get response from Groq API | |
response = get_groq_response(user_input) | |
st.session_state.conversation.append(f"Bot: {response}") | |
# Display the conversation history | |
for message in st.session_state.conversation: | |
st.write(message) | |