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