Spaces:
Sleeping
Sleeping
Create app2.py
Browse files
app2.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Simple Chatbot
|
| 2 |
+
@author: Nigel Gebodh
|
| 3 |
+
@email: nigel.gebodh@gmail.com
|
| 4 |
+
"""
|
| 5 |
+
import numpy as np
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from openai import OpenAI
|
| 8 |
+
import os
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Initialize the client
|
| 14 |
+
client = OpenAI(
|
| 15 |
+
base_url="https://api-inference.huggingface.co/v1",
|
| 16 |
+
api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN') # Replace with your token
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Define Llama 3 model
|
| 20 |
+
model_link = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 21 |
+
model_info = {
|
| 22 |
+
'description': """The Llama (3) model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n
|
| 23 |
+
It was created by the [**Meta's AI**](https://llama.meta.com/) team and has over **8 billion parameters.** \n""",
|
| 24 |
+
'logo': 'Llama_logo.png'
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Random dog images for error message
|
| 28 |
+
random_dog = ["0f476473-2d8b-415e-b944-483768418a95.jpg",
|
| 29 |
+
"1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
|
| 30 |
+
"526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
|
| 31 |
+
"1326984c-39b0-492c-a773-f120d747a7e2.jpg"]
|
| 32 |
+
|
| 33 |
+
def reset_conversation():
|
| 34 |
+
'''Resets Conversation'''
|
| 35 |
+
st.session_state.conversation = []
|
| 36 |
+
st.session_state.messages = []
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
# Create a temperature slider
|
| 40 |
+
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
|
| 41 |
+
|
| 42 |
+
# Add reset button to clear conversation
|
| 43 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation) # Reset button
|
| 44 |
+
|
| 45 |
+
# Create model description
|
| 46 |
+
st.sidebar.write(f"You're now chatting with **Llama 3**")
|
| 47 |
+
st.sidebar.markdown(model_info['description'])
|
| 48 |
+
st.sidebar.image(model_info['logo'])
|
| 49 |
+
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
| 50 |
+
st.sidebar.markdown("\nRun into issues? \nTry again later as GPU access might be limited.")
|
| 51 |
+
|
| 52 |
+
# Initialize chat history
|
| 53 |
+
if "messages" not in st.session_state:
|
| 54 |
+
st.session_state.messages = []
|
| 55 |
+
|
| 56 |
+
# Display chat messages from history on app rerun
|
| 57 |
+
for message in st.session_state.messages:
|
| 58 |
+
with st.chat_message(message["role"]):
|
| 59 |
+
st.markdown(message["content"])
|
| 60 |
+
|
| 61 |
+
# Accept user input
|
| 62 |
+
if prompt := st.chat_input(f"Hi, I'm Llama 3, ask me a question"):
|
| 63 |
+
|
| 64 |
+
# Display user message in chat message container
|
| 65 |
+
with st.chat_message("user"):
|
| 66 |
+
st.markdown(prompt)
|
| 67 |
+
# Add user message to chat history
|
| 68 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 69 |
+
|
| 70 |
+
# Display assistant response in chat message container
|
| 71 |
+
with st.chat_message("assistant"):
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
stream = client.chat.completions.create(
|
| 75 |
+
model=model_link,
|
| 76 |
+
messages=[
|
| 77 |
+
{"role": m["role"], "content": m["content"]}
|
| 78 |
+
for m in st.session_state.messages
|
| 79 |
+
],
|
| 80 |
+
temperature=temp_values,
|
| 81 |
+
stream=True,
|
| 82 |
+
max_tokens=3000,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
response = st.write_stream(stream)
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
response = "šµāš« Looks like something went wrong! Try again later.\nHere's a random pic of a š¶:"
|
| 89 |
+
st.write(response)
|
| 90 |
+
random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
|
| 91 |
+
st.image(random_dog_pick)
|
| 92 |
+
st.write("This was the error message:")
|
| 93 |
+
st.write(e)
|
| 94 |
+
|
| 95 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|