Spaces:
Sleeping
Sleeping
embedded GROQ
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Hide Streamlit menu and footer
|
5 |
+
hide_menu_style = """
|
6 |
+
<style>
|
7 |
+
#MainMenu {visibility: hidden;}
|
8 |
+
footer {visibility: hidden;}
|
9 |
+
</style>
|
10 |
+
"""
|
11 |
+
st.markdown(hide_menu_style, unsafe_allow_html=True)
|
12 |
+
|
13 |
+
class VietnameseChatbot:
|
14 |
+
def __init__(self):
|
15 |
+
self.api_key = st.secrets["GROQ_API_KEY"] # Store your API key in Huggingface Secrets
|
16 |
+
self.api_url = "https://api.groq.com/openai/v1/chat/completions"
|
17 |
+
self.headers = {
|
18 |
+
"Content-Type": "application/json",
|
19 |
+
"Authorization": f"Bearer {self.api_key}"
|
20 |
+
}
|
21 |
+
|
22 |
+
def get_response(self, user_query):
|
23 |
+
try:
|
24 |
+
payload = {
|
25 |
+
"model": "gemma2-9b-it",
|
26 |
+
"messages": [
|
27 |
+
{"role": "user", "content": user_query}
|
28 |
+
]
|
29 |
+
}
|
30 |
+
|
31 |
+
response = requests.post(
|
32 |
+
self.api_url,
|
33 |
+
headers=self.headers,
|
34 |
+
json=payload
|
35 |
+
)
|
36 |
+
|
37 |
+
if response.status_code == 200:
|
38 |
+
return response.json()['choices'][0]['message']['content']
|
39 |
+
else:
|
40 |
+
print(f"API Error: {response.status_code}")
|
41 |
+
print(f"Response: {response.text}")
|
42 |
+
return "Đã xảy ra lỗi khi kết nối với API. Xin vui lòng thử lại."
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Response generation error: {e}")
|
46 |
+
return "Đã xảy ra lỗi. Xin vui lòng thử lại."
|
47 |
+
|
48 |
+
@st.cache_resource
|
49 |
+
def initialize_chatbot():
|
50 |
+
return VietnameseChatbot()
|
51 |
+
|
52 |
+
def main():
|
53 |
+
st.set_page_config(
|
54 |
+
page_title="IOGPT",
|
55 |
+
page_icon="🤖",
|
56 |
+
menu_items={} # This helps hide the menu
|
57 |
+
)
|
58 |
+
|
59 |
+
st.title("🤖 Trợ Lý AI - IOGPT")
|
60 |
+
st.caption("Trò chuyện với chúng mình nhé!")
|
61 |
+
|
62 |
+
# Initialize chatbot using cached initialization
|
63 |
+
chatbot = initialize_chatbot()
|
64 |
+
|
65 |
+
# Chat history in session state
|
66 |
+
if 'messages' not in st.session_state:
|
67 |
+
st.session_state.messages = []
|
68 |
+
|
69 |
+
# Display chat messages
|
70 |
+
for message in st.session_state.messages:
|
71 |
+
with st.chat_message(message["role"]):
|
72 |
+
st.markdown(message["content"])
|
73 |
+
|
74 |
+
# User input
|
75 |
+
if prompt := st.chat_input("Hãy nói gì đó..."):
|
76 |
+
# Add user message to chat history
|
77 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
78 |
+
|
79 |
+
# Display user message
|
80 |
+
with st.chat_message("user"):
|
81 |
+
st.markdown(prompt)
|
82 |
+
|
83 |
+
# Get chatbot response
|
84 |
+
response = chatbot.get_response(prompt)
|
85 |
+
|
86 |
+
# Display chatbot response
|
87 |
+
with st.chat_message("assistant"):
|
88 |
+
st.markdown(response)
|
89 |
+
|
90 |
+
# Add assistant message to chat history
|
91 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
92 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
main()
|