Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 🔐 Get secret from Hugging Face (NOT hardcoded)
|
| 6 |
+
API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
|
| 8 |
+
API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 9 |
+
|
| 10 |
+
st.set_page_config(page_title="Groq AI Chatbot", layout="centered")
|
| 11 |
+
|
| 12 |
+
st.title("AI Chatbot (Groq)")
|
| 13 |
+
|
| 14 |
+
if "messages" not in st.session_state:
|
| 15 |
+
st.session_state.messages = []
|
| 16 |
+
|
| 17 |
+
# Show chat history
|
| 18 |
+
for msg in st.session_state.messages:
|
| 19 |
+
with st.chat_message(msg["role"]):
|
| 20 |
+
st.write(msg["content"])
|
| 21 |
+
|
| 22 |
+
def get_response(messages):
|
| 23 |
+
headers = {
|
| 24 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 25 |
+
"Content-Type": "application/json"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
payload = {
|
| 29 |
+
"model": "llama-3.1-8b-instant",
|
| 30 |
+
"messages": messages,
|
| 31 |
+
"temperature": 0.7
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
res = requests.post(API_URL, headers=headers, json=payload)
|
| 35 |
+
|
| 36 |
+
if res.status_code == 200:
|
| 37 |
+
return res.json()["choices"][0]["message"]["content"]
|
| 38 |
+
else:
|
| 39 |
+
return res.text
|
| 40 |
+
|
| 41 |
+
user_input = st.chat_input("Type your message...")
|
| 42 |
+
|
| 43 |
+
if user_input:
|
| 44 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 45 |
+
|
| 46 |
+
reply = get_response(st.session_state.messages)
|
| 47 |
+
|
| 48 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 49 |
+
|
| 50 |
+
st.rerun()
|