File size: 2,417 Bytes
7e0b3c1
 
b1c5080
 
7e0b3c1
 
b1c5080
218f566
b1c5080
 
7e0b3c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1c5080
 
 
 
 
 
 
 
7e0b3c1
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import streamlit as st
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import os

# Load the model and tokenizer
model_name = "EleutherAI/gpt-neo-125m"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Random dog images for error messages
random_dog = [
    "0f476473-2d8b-415e-b944-483768418a95.jpg",
    "1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
    # Add more images as needed
]

def reset_conversation():
    '''Resets conversation'''
    st.session_state.conversation = []
    st.session_state.messages = []
    return None

# Create sidebar controls
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5)
max_token_value = st.sidebar.slider('Select a max_token value', 1000, 9000, 5000)
st.sidebar.button('Reset Chat', on_click=reset_conversation)

# Set the model and display its name
st.sidebar.write(f"You're now chatting with **{model_name}**")
st.sidebar.markdown("*Generated content may be inaccurate or false.*")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Accept user input
if prompt := st.chat_input(f"Hi, I'm {model_name}, ask me a question"):
    with st.chat_message("user"):
        st.markdown(prompt)
    st.session_state.messages.append({"role": "user", "content": prompt})

    # Display assistant response
    with st.chat_message("assistant"):
        try:
            inputs = tokenizer(prompt, return_tensors="pt")
            outputs = model.generate(
                inputs.input_ids,
                max_new_tokens=max_token_value,
                temperature=temp_values,
                do_sample=True
            )
            assistant_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        except Exception as e:
            assistant_response = "šŸ˜µā€šŸ’« Connection issue! Try again later. Here's a 🐶:"
            st.image(f'https://random.dog/{random_dog[np.random.randint(len(random_dog))]}')
            st.write("Error message:")
            st.write(e)

    st.markdown(assistant_response)
    st.session_state.messages.append({"role": "assistant", "content": assistant_response})