Spaces:
Running
Running
S.Sai Yashasvini
commited on
main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from groq import Groq
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# streamlit page configuration
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="LLAMA 3.1. Chat",
|
| 11 |
+
page_icon="🦙",
|
| 12 |
+
layout="centered"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
working_dir = os.path.dirname(os.path.abspath(__file__))
|
| 16 |
+
config_data = json.load(open(f"{working_dir}/config.json"))
|
| 17 |
+
|
| 18 |
+
GROQ_API_KEY = config_data["GROQ_API_KEY"]
|
| 19 |
+
|
| 20 |
+
# save the api key to environment variable
|
| 21 |
+
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 22 |
+
|
| 23 |
+
client = Groq()
|
| 24 |
+
|
| 25 |
+
# initialize the chat history as streamlit session state of not present already
|
| 26 |
+
if "chat_history" not in st.session_state:
|
| 27 |
+
st.session_state.chat_history = []
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# streamlit page title
|
| 31 |
+
st.title("🦙 LLAMA 3.1. ChatBot")
|
| 32 |
+
|
| 33 |
+
# display chat history
|
| 34 |
+
for message in st.session_state.chat_history:
|
| 35 |
+
with st.chat_message(message["role"]):
|
| 36 |
+
st.markdown(message["content"])
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# input field for user's message:
|
| 40 |
+
user_prompt = st.chat_input("Ask LLAMA...")
|
| 41 |
+
|
| 42 |
+
if user_prompt:
|
| 43 |
+
|
| 44 |
+
st.chat_message("user").markdown(user_prompt)
|
| 45 |
+
st.session_state.chat_history.append({"role": "user", "content": user_prompt})
|
| 46 |
+
|
| 47 |
+
# sens user's message to the LLM and get a response
|
| 48 |
+
messages = [
|
| 49 |
+
{"role": "system", "content": "You are a helpful assistant"},
|
| 50 |
+
*st.session_state.chat_history
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
response = client.chat.completions.create(
|
| 54 |
+
model="llama-3.1-8b-instant",
|
| 55 |
+
messages=messages
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
assistant_response = response.choices[0].message.content
|
| 59 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 60 |
+
|
| 61 |
+
# display the LLM's response
|
| 62 |
+
with st.chat_message("assistant"):
|
| 63 |
+
st.markdown(assistant_response)
|