Spaces:
No application file
No application file
import os | |
import streamlit as st | |
from groq import Client | |
# Function to initialize Groq client | |
def get_groq_client(): | |
api_key = "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" | |
if not api_key: | |
st.error("API key is missing. Please set the GROQ_API_KEY environment variable.") | |
return None | |
return Client(api_key=api_key) | |
# Function to get chatbot response | |
def get_response(client, user_input): | |
try: | |
response = client.chat.completions.create( | |
messages=[{"role": "user", "content": user_input}], | |
model="llama-3.3-70b-versatile" | |
) | |
return response['choices'][0]['message']['content'] | |
except Exception as e: | |
st.error(f"Error: {e}") | |
return None | |
# Streamlit UI | |
def main(): | |
st.title("AI Chatbot using Groq") | |
# Get the Groq client | |
client = get_groq_client() | |
if not client: | |
return | |
# User input | |
user_input = st.text_input("Ask a question:") | |
if st.button("Get Answer"): | |
if user_input: | |
with st.spinner("Thinking..."): | |
answer = get_response(client, user_input) | |
if answer: | |
st.success("Response:") | |
st.write(answer) | |
else: | |
st.warning("Please enter a message.") | |
if __name__ == "__main__": | |
main() | |