Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
gpt2_tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 9 |
|
| 10 |
-
def chat(message
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
conversation = [{"role": "system", "content": "You are a helpful assistant."},
|
| 16 |
-
{"role": "user", "content": message}]
|
| 17 |
|
| 18 |
-
if history:
|
| 19 |
-
conversation.append({"role": "assistant", "content": history})
|
| 20 |
-
|
| 21 |
# Generate a response from DialoGPT-medium
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return bot_response
|
| 26 |
|
| 27 |
# Create and launch the Gradio interface
|
| 28 |
-
iface = gr.
|
| 29 |
-
chat,
|
| 30 |
title="UrFriendly Chatbot",
|
| 31 |
-
description="UrFriendly Chatbot is a conversational assistant based on DialoGPT-medium
|
| 32 |
examples=[
|
| 33 |
"Howdy!",
|
| 34 |
"Tell me a joke.",
|
|
@@ -37,10 +36,9 @@ iface = gr.ChatInterface(
|
|
| 37 |
"What is an exponent in mathematics?",
|
| 38 |
"Does money buy happiness?"
|
| 39 |
],
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
)
|
| 44 |
|
| 45 |
-
iface.queue()
|
| 46 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
|
| 4 |
+
# Initialize the DialoGPT model and tokenizer
|
| 5 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 7 |
|
| 8 |
+
chat_history = None
|
|
|
|
| 9 |
|
| 10 |
+
def chat(message):
|
| 11 |
+
global chat_history
|
| 12 |
+
|
| 13 |
+
# Encode the user's message with the GPT-2 tokenizer
|
| 14 |
+
input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt")
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
# Generate a response from DialoGPT-medium
|
| 17 |
+
response_ids = model.generate(input_ids, max_length=150, pad_token_id=tokenizer.eos_token_id, num_return_sequences=1)
|
| 18 |
+
|
| 19 |
+
# Decode and return the bot's response
|
| 20 |
+
bot_response = tokenizer.decode(response_ids[0], skip_special_tokens=True)
|
| 21 |
+
|
| 22 |
+
chat_history = bot_response # Store the bot's response for reference
|
| 23 |
+
|
| 24 |
return bot_response
|
| 25 |
|
| 26 |
# Create and launch the Gradio interface
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=chat,
|
| 29 |
title="UrFriendly Chatbot",
|
| 30 |
+
description="UrFriendly Chatbot is a conversational assistant based on DialoGPT-medium with GPT-2 tokenization. Type or click on one of the examples to get started. Please note that UrFriendly Chatbot is not 100% accurate, so incorrect information may generate. π¬π€",
|
| 31 |
examples=[
|
| 32 |
"Howdy!",
|
| 33 |
"Tell me a joke.",
|
|
|
|
| 36 |
"What is an exponent in mathematics?",
|
| 37 |
"Does money buy happiness?"
|
| 38 |
],
|
| 39 |
+
inputs="text",
|
| 40 |
+
outputs="text",
|
| 41 |
+
live=True # Set to True to allow continuous conversation
|
| 42 |
)
|
| 43 |
|
|
|
|
| 44 |
iface.launch()
|