Spaces:
No application file
No application file
File size: 1,329 Bytes
26ce7c2 ca18edf 8d3b68e ca18edf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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()
|