Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Sidebar for selecting the model and entering the API key
|
| 10 |
+
st.sidebar.title("Model Selection")
|
| 11 |
+
model_name = st.sidebar.selectbox("Select a model", ["llama3-70b-8192"])
|
| 12 |
+
api_key = st.sidebar.text_input("Enter your Groq API key", type="password")
|
| 13 |
+
|
| 14 |
+
# Initialize the Groq client
|
| 15 |
+
client = Groq(api_key=api_key)
|
| 16 |
+
|
| 17 |
+
if api_key:
|
| 18 |
+
client = Groq(api_key=api_key)
|
| 19 |
+
|
| 20 |
+
# Chat interface
|
| 21 |
+
st.title("Chatbot")
|
| 22 |
+
|
| 23 |
+
msgs = StreamlitChatMessageHistory(key="special_app_key")
|
| 24 |
+
|
| 25 |
+
for msg in msgs.messages:
|
| 26 |
+
st.chat_message(msg.type).write(msg.content)
|
| 27 |
+
|
| 28 |
+
if prompt := st.chat_input():
|
| 29 |
+
start_time = time.time()
|
| 30 |
+
st.chat_message("human").write(prompt)
|
| 31 |
+
msgs.add_user_message(prompt)
|
| 32 |
+
|
| 33 |
+
with st.spinner("Waiting for response..."):
|
| 34 |
+
chat_completion = client.chat.completions.create(
|
| 35 |
+
messages=[{"role": "user", "content": prompt}],
|
| 36 |
+
model=model_name,
|
| 37 |
+
)
|
| 38 |
+
res = chat_completion.choices[0].message.content
|
| 39 |
+
|
| 40 |
+
if res:
|
| 41 |
+
st.chat_message("ai").write(res)
|
| 42 |
+
end_time = time.time()
|
| 43 |
+
print(f"Total time {end_time-start_time}")
|
| 44 |
+
msgs.add_ai_message(res)
|
| 45 |
+
else:
|
| 46 |
+
st.error("No valid response received from the AI.")
|