Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
+
|
4 |
+
# Load the GPT-2 model and tokenizer
|
5 |
+
@st.cache_resource
|
6 |
+
def load_model():
|
7 |
+
model_name = "gpt2"
|
8 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
9 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
10 |
+
return model, tokenizer
|
11 |
+
|
12 |
+
# Function to generate a response from GPT-2
|
13 |
+
def generate_response(input_text, model, tokenizer):
|
14 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
15 |
+
outputs = model.generate(inputs, max_length=150, do_sample=True, top_p=0.9, top_k=50)
|
16 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
17 |
+
return response
|
18 |
+
|
19 |
+
# Streamlit UI setup
|
20 |
+
def main():
|
21 |
+
st.title("GPT-2 Chatbot")
|
22 |
+
|
23 |
+
# Chat history
|
24 |
+
if 'history' not in st.session_state:
|
25 |
+
st.session_state['history'] = []
|
26 |
+
|
27 |
+
user_input = st.text_input("You:", "")
|
28 |
+
|
29 |
+
# Generate and display response
|
30 |
+
if user_input:
|
31 |
+
model, tokenizer = load_model()
|
32 |
+
response = generate_response(user_input, model, tokenizer)
|
33 |
+
st.session_state['history'].append({"user": user_input, "bot": response})
|
34 |
+
|
35 |
+
# Display chat history
|
36 |
+
for chat in st.session_state['history']:
|
37 |
+
st.write(f"You: {chat['user']}")
|
38 |
+
st.write(f"Bot: {chat['bot']}")
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
main()
|