Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Load the model and tokenizer (e.g., a GPT model from Hugging Face)
|
| 5 |
+
@st.cache_resource
|
| 6 |
+
def load_model():
|
| 7 |
+
model_name = "microsoft/DialoGPT-small" # Replace with your chosen model
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
+
return tokenizer, model
|
| 11 |
+
|
| 12 |
+
# Initialize model and tokenizer
|
| 13 |
+
tokenizer, model = load_model()
|
| 14 |
+
|
| 15 |
+
# Streamlit app layout
|
| 16 |
+
st.title("🤖 Friendly Chatbot")
|
| 17 |
+
st.write("Hi! I'm your friendly chatbot. Ask me anything or just say hello! 😊")
|
| 18 |
+
|
| 19 |
+
# User input
|
| 20 |
+
user_input = st.text_input("You:", placeholder="Type your message here...")
|
| 21 |
+
|
| 22 |
+
# Generate response
|
| 23 |
+
if user_input:
|
| 24 |
+
inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
| 25 |
+
response = model.generate(inputs, max_length=50, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
|
| 26 |
+
bot_reply = tokenizer.decode(response[0], skip_special_tokens=True)
|
| 27 |
+
st.text_area("Chatbot:", value=bot_reply, height=100)
|
| 28 |
+
|