Spaces:
Sleeping
Sleeping
Dua Rajper commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
+
if not GROQ_API_KEY:
|
| 10 |
+
st.error("GROQ_API_KEY not found. Please set it before running the app.")
|
| 11 |
+
st.stop()
|
| 12 |
+
|
| 13 |
+
# Initialize the Groq client
|
| 14 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 15 |
+
|
| 16 |
+
def get_groq_response(user_input):
|
| 17 |
+
try:
|
| 18 |
+
chat_completion = client.chat.completions.create(
|
| 19 |
+
messages=[
|
| 20 |
+
{"role": "user", "content": user_input}
|
| 21 |
+
],
|
| 22 |
+
model="llama-3.3-70b-versatile",
|
| 23 |
+
)
|
| 24 |
+
return chat_completion.choices[0].message.content
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error: {str(e)}"
|
| 27 |
+
|
| 28 |
+
# Streamlit UI
|
| 29 |
+
st.title("Hugging Face + GROQ API Chatbot")
|
| 30 |
+
user_input = st.text_area("Enter your message:")
|
| 31 |
+
|
| 32 |
+
if st.button("Generate Response"):
|
| 33 |
+
if user_input:
|
| 34 |
+
response = get_groq_response(user_input)
|
| 35 |
+
st.write("**GROQ Response:**")
|
| 36 |
+
st.write(response)
|
| 37 |
+
else:
|
| 38 |
+
st.warning("Please enter a message before submitting.")
|