Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,24 +3,23 @@ import gradio as gr
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
|
| 8 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
|
| 9 |
|
| 10 |
-
# Define
|
| 11 |
def chat_with_bot(message, history):
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
bot_input_ids = torch.cat([bot_input_ids, previous], dim=-1)
|
| 20 |
|
| 21 |
-
# Generate response
|
| 22 |
output_ids = model.generate(
|
| 23 |
-
|
| 24 |
max_length=1000,
|
| 25 |
pad_token_id=tokenizer.eos_token_id,
|
| 26 |
do_sample=True,
|
|
@@ -28,14 +27,12 @@ def chat_with_bot(message, history):
|
|
| 28 |
top_p=0.95
|
| 29 |
)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
response = tokenizer.decode(output_ids[:,
|
| 33 |
-
|
| 34 |
-
# Append to chat history and return
|
| 35 |
history.append((message, response))
|
| 36 |
return response, history
|
| 37 |
|
| 38 |
-
# Launch
|
| 39 |
gr.ChatInterface(
|
| 40 |
fn=chat_with_bot,
|
| 41 |
title="Muhammad’s ChatGPT",
|
|
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Load DialoGPT model
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
|
| 8 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
|
| 9 |
|
| 10 |
+
# Define the chatbot function
|
| 11 |
def chat_with_bot(message, history):
|
| 12 |
+
# Build the conversation history into input_ids
|
| 13 |
+
input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt")
|
| 14 |
|
| 15 |
+
for user_msg, bot_msg in history:
|
| 16 |
+
user_ids = tokenizer.encode(user_msg + tokenizer.eos_token, return_tensors="pt")
|
| 17 |
+
bot_ids = tokenizer.encode(bot_msg + tokenizer.eos_token, return_tensors="pt")
|
| 18 |
+
input_ids = torch.cat([user_ids, bot_ids, input_ids], dim=-1)
|
|
|
|
| 19 |
|
| 20 |
+
# Generate a response
|
| 21 |
output_ids = model.generate(
|
| 22 |
+
input_ids,
|
| 23 |
max_length=1000,
|
| 24 |
pad_token_id=tokenizer.eos_token_id,
|
| 25 |
do_sample=True,
|
|
|
|
| 27 |
top_p=0.95
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# Decode and return the response
|
| 31 |
+
response = tokenizer.decode(output_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 32 |
history.append((message, response))
|
| 33 |
return response, history
|
| 34 |
|
| 35 |
+
# Launch the Gradio ChatInterface
|
| 36 |
gr.ChatInterface(
|
| 37 |
fn=chat_with_bot,
|
| 38 |
title="Muhammad’s ChatGPT",
|