Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
"text-generation",
|
| 7 |
-
model="microsoft/DialoGPT-small"
|
| 8 |
-
max_length=200,
|
| 9 |
-
pad_token_id=50256
|
| 10 |
)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
prompt_text += f"User: {user}\nBot: {bot}\n"
|
| 17 |
-
prompt_text += f"User: {message}\nBot:"
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load the model
|
| 5 |
+
chatbot_pipeline = pipeline(
|
| 6 |
"text-generation",
|
| 7 |
+
model="microsoft/DialoGPT-small"
|
|
|
|
|
|
|
| 8 |
)
|
| 9 |
|
| 10 |
+
# Define a function to handle the conversation
|
| 11 |
+
def respond(message, history=None):
|
| 12 |
+
if history is None:
|
| 13 |
+
history = []
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Merge previous messages into a single conversation string
|
| 16 |
+
conversation = ""
|
| 17 |
+
for user_msg, bot_msg in history:
|
| 18 |
+
conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
|
| 19 |
|
| 20 |
+
conversation += f"User: {message}\nBot:"
|
| 21 |
+
|
| 22 |
+
# Generate response
|
| 23 |
+
output = chatbot_pipeline(
|
| 24 |
+
conversation,
|
| 25 |
+
max_length=200,
|
| 26 |
+
pad_token_id=50256
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
reply = output[0]['generated_text'].split("Bot:")[-1].strip()
|
| 30 |
+
history.append((message, reply))
|
| 31 |
+
return reply, history
|
| 32 |
|
| 33 |
+
# Create the Gradio interface
|
| 34 |
+
gr.ChatInterface(fn=respond).launch()
|