Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,50 @@
|
|
| 1 |
-
from transformers import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
title = "🤖AI ChatBot"
|
| 6 |
+
description = "A State-of-the-Art Large-scale Pretrained Response generation model (gpt-neo-1.3B)"
|
| 7 |
+
examples = [["How are you?"]]
|
| 8 |
+
|
| 9 |
+
# Use the better model and tokenizer
|
| 10 |
+
model_name = "EleutherAI/gpt-neo-1.3B"
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
def predict(input_text, history=None):
|
| 15 |
+
if history is None:
|
| 16 |
+
history = []
|
| 17 |
+
|
| 18 |
+
# Tokenize the new input sentence
|
| 19 |
+
new_user_input_ids = tokenizer.encode(
|
| 20 |
+
input_text + tokenizer.eos_token, return_tensors="pt"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Append the new user input tokens to the chat history
|
| 24 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
| 25 |
+
|
| 26 |
+
# Generate a response using batch processing
|
| 27 |
+
generated_ids = model.generate(
|
| 28 |
+
bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Convert the generated response tokens to text
|
| 32 |
+
response = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 33 |
+
|
| 34 |
+
# Split the responses into lines
|
| 35 |
+
response = response.split("\n")
|
| 36 |
+
|
| 37 |
+
# Convert to tuples of list
|
| 38 |
+
response = [(response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)]
|
| 39 |
+
|
| 40 |
+
return response, generated_ids.tolist()
|
| 41 |
+
|
| 42 |
+
gr.Interface(
|
| 43 |
+
fn=predict,
|
| 44 |
+
title=title,
|
| 45 |
+
description=description,
|
| 46 |
+
examples=examples,
|
| 47 |
+
inputs=["text", "state"],
|
| 48 |
+
outputs=["chatbot", "state"],
|
| 49 |
+
theme="finlaymacklon/boxy_violet",
|
| 50 |
+
).launch()
|