Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,23 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import torch
|
| 4 |
from openai import OpenAI
|
| 5 |
-
import numpy as np
|
| 6 |
-
import sys
|
| 7 |
-
from dotenv import load_dotenv
|
| 8 |
import random
|
|
|
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
|
| 11 |
-
|
| 12 |
# Load environment variables
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
# Constants
|
| 20 |
MAX_TOKENS = 4000
|
| 21 |
DEFAULT_TEMPERATURE = 0.5
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
client = OpenAI(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
)
|
| 29 |
-
|
| 30 |
# Create supported models
|
| 31 |
model_links = {
|
| 32 |
"Meta-Llama-3.1-8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
|
@@ -46,12 +37,11 @@ def reset_conversation():
|
|
| 46 |
st.session_state.messages = []
|
| 47 |
return None
|
| 48 |
|
| 49 |
-
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
| 50 |
-
|
| 51 |
def main():
|
| 52 |
st.header('Multi-Models')
|
| 53 |
|
| 54 |
-
|
| 55 |
# Sidebar for model selection and temperature
|
| 56 |
selected_model = st.sidebar.selectbox("Select Model", list(model_links.keys()))
|
| 57 |
temperature = st.sidebar.slider('Select a temperature value', 0.0, 1.0, DEFAULT_TEMPERATURE)
|
|
@@ -61,7 +51,6 @@ def main():
|
|
| 61 |
|
| 62 |
if st.session_state.prev_option != selected_model:
|
| 63 |
st.session_state.messages = []
|
| 64 |
-
# st.write(f"Changed to {selected_model}")
|
| 65 |
st.session_state.prev_option = selected_model
|
| 66 |
reset_conversation()
|
| 67 |
|
|
@@ -71,9 +60,11 @@ def main():
|
|
| 71 |
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
| 72 |
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# Chat input and response
|
| 79 |
if prompt := st.chat_input("Type message here..."):
|
|
@@ -85,13 +76,16 @@ def process_user_input(client, prompt, selected_model, temperature):
|
|
| 85 |
st.markdown(prompt)
|
| 86 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 87 |
|
| 88 |
-
|
| 89 |
# Generate and display assistant response
|
| 90 |
with st.chat_message("assistant"):
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
| 3 |
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
| 4 |
import random
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
|
|
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Constants
|
| 12 |
MAX_TOKENS = 4000
|
| 13 |
DEFAULT_TEMPERATURE = 0.5
|
| 14 |
|
| 15 |
+
# Initialize the client
|
|
|
|
| 16 |
client = OpenAI(
|
| 17 |
+
base_url="https://api-inference.huggingface.co/v1",
|
| 18 |
+
api_key=os.environ.get('API_KEY') # Replace with your token
|
| 19 |
)
|
| 20 |
+
|
| 21 |
# Create supported models
|
| 22 |
model_links = {
|
| 23 |
"Meta-Llama-3.1-8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
|
|
|
| 37 |
st.session_state.messages = []
|
| 38 |
return None
|
| 39 |
|
| 40 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation) # Reset button
|
| 41 |
+
|
| 42 |
def main():
|
| 43 |
st.header('Multi-Models')
|
| 44 |
|
|
|
|
| 45 |
# Sidebar for model selection and temperature
|
| 46 |
selected_model = st.sidebar.selectbox("Select Model", list(model_links.keys()))
|
| 47 |
temperature = st.sidebar.slider('Select a temperature value', 0.0, 1.0, DEFAULT_TEMPERATURE)
|
|
|
|
| 51 |
|
| 52 |
if st.session_state.prev_option != selected_model:
|
| 53 |
st.session_state.messages = []
|
|
|
|
| 54 |
st.session_state.prev_option = selected_model
|
| 55 |
reset_conversation()
|
| 56 |
|
|
|
|
| 60 |
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
| 61 |
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
| 62 |
|
| 63 |
+
# Add a placeholder for chat messages
|
| 64 |
+
if "messages" in st.session_state:
|
| 65 |
+
for message in st.session_state.messages:
|
| 66 |
+
with st.chat_message(message["role"]):
|
| 67 |
+
st.markdown(message["content"])
|
| 68 |
|
| 69 |
# Chat input and response
|
| 70 |
if prompt := st.chat_input("Type message here..."):
|
|
|
|
| 76 |
st.markdown(prompt)
|
| 77 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 78 |
|
|
|
|
| 79 |
# Generate and display assistant response
|
| 80 |
with st.chat_message("assistant"):
|
| 81 |
+
response = """😵💫 Looks like someone unplugged something!
|
| 82 |
+
\n Either the model space is being updated or something is down."""
|
| 83 |
+
st.write(response)
|
| 84 |
+
random_dog_pick = random.choice(random_dog_images)
|
| 85 |
+
st.image(random_dog_pick)
|
| 86 |
+
st.write("This was the error message:")
|
| 87 |
+
# Replace with the actual error handling
|
| 88 |
+
st.write("Error details would go here.")
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
main()
|