Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,51 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Create a text-generation pipeline (no Conversation class needed)
|
| 5 |
+
model_name = "Decryptu/distilgpt2"
|
| 6 |
+
generator = pipeline("text-generation", model=model_name)
|
| 7 |
+
|
| 8 |
+
# Store chat history in session_state
|
| 9 |
+
if "history" not in st.session_state:
|
| 10 |
+
st.session_state.history = []
|
| 11 |
+
|
| 12 |
+
st.title("Chat with DistilGPT2")
|
| 13 |
+
st.write("A simple chat-like interface using a text-generation pipeline.")
|
| 14 |
+
|
| 15 |
+
# User input
|
| 16 |
+
user_input = st.text_input("Type your message here:")
|
| 17 |
+
|
| 18 |
+
# On 'Send' button click
|
| 19 |
+
if st.button("Send"):
|
| 20 |
+
if user_input.strip():
|
| 21 |
+
# 1) Append user's message to the history
|
| 22 |
+
st.session_state.history.append(("User", user_input))
|
| 23 |
+
|
| 24 |
+
# 2) Build a prompt from the entire conversation
|
| 25 |
+
prompt = ""
|
| 26 |
+
for speaker, text in st.session_state.history:
|
| 27 |
+
prompt += f"{speaker}: {text}\n"
|
| 28 |
+
prompt += "Bot: "
|
| 29 |
+
|
| 30 |
+
# 3) Generate text from the model
|
| 31 |
+
output = generator(
|
| 32 |
+
prompt,
|
| 33 |
+
max_length=100,
|
| 34 |
+
do_sample=True,
|
| 35 |
+
temperature=0.7
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# The model output includes the prompt plus new text
|
| 39 |
+
bot_text = output[0]["generated_text"].split("Bot:", 1)[-1].strip()
|
| 40 |
+
|
| 41 |
+
# 4) Append the bot's response to the history
|
| 42 |
+
st.session_state.history.append(("Bot", bot_text))
|
| 43 |
+
|
| 44 |
+
# Display the conversation
|
| 45 |
+
st.write("### Conversation")
|
| 46 |
+
for speaker, text in st.session_state.history:
|
| 47 |
+
st.write(f"**{speaker}:** {text}")
|
| 48 |
+
|
| 49 |
+
# Add a 'Clear Chat' button (optional)
|
| 50 |
+
if st.button("Clear Chat"):
|
| 51 |
+
st.session_state.history = []
|