Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
return "", history + [[message, None]]
|
| 11 |
|
| 12 |
-
|
| 13 |
-
def bot(history):
|
| 14 |
-
user_message = history[-1][0]
|
| 15 |
-
new_user_input_ids = tokenizer.encode(
|
| 16 |
-
user_message + tokenizer.eos_token, return_tensors="pt"
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
# append the new user input tokens to the chat history
|
| 20 |
-
bot_input_ids = torch.cat([torch.LongTensor([]), new_user_input_ids], dim=-1)
|
| 21 |
-
|
| 22 |
-
# generate a response
|
| 23 |
-
response = model.generate(
|
| 24 |
-
bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id
|
| 25 |
-
).tolist()
|
| 26 |
-
|
| 27 |
-
# convert the tokens to text, and then split the responses into lines
|
| 28 |
-
response = tokenizer.decode(response[0]).split("<|endoftext|>")
|
| 29 |
-
response = [
|
| 30 |
-
(response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
|
| 31 |
-
] # convert to tuples of list
|
| 32 |
-
history[-1] = response[0]
|
| 33 |
-
return history
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
with gr.Blocks() as demo:
|
| 37 |
-
chatbot = gr.Chatbot()
|
| 38 |
-
msg = gr.Textbox()
|
| 39 |
-
clear = gr.Button("Clear")
|
| 40 |
-
|
| 41 |
-
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 42 |
-
bot, chatbot, chatbot
|
| 43 |
-
)
|
| 44 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 45 |
|
| 46 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
+
generator = pipeline('text-generation', model = 'gpt2')
|
|
|
|
| 5 |
|
| 6 |
+
def generate_text(text_prompt):
|
| 7 |
+
response = generator(text_prompt, max_length = 30, num_return_sequences=5)
|
| 8 |
+
return response[0]['generated_text']
|
| 9 |
|
| 10 |
+
textbox = gr.Textbox()
|
|
|
|
| 11 |
|
| 12 |
+
demo = gr.Interface(generate_text, textbox, textbox)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
demo.launch()
|