Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,29 @@
|
|
| 1 |
-
from
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
# Chat logic
|
| 12 |
def chat(message, history):
|
|
|
|
| 13 |
full_prompt = ""
|
| 14 |
for user, bot in history:
|
| 15 |
full_prompt += f"User: {user}\nBot: {bot}\n"
|
| 16 |
full_prompt += f"User: {message}\nBot:"
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return reply
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
gr.ChatInterface(fn=chat, title="
|
| 24 |
-
server_name="0.0.0.0", server_port=7860
|
| 25 |
-
)
|
|
|
|
| 1 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# Load the tokenizer and model from Hugging Face
|
| 5 |
+
tokenizer = GPT2Tokenizer.from_pretrained("distilgpt2")
|
| 6 |
+
model = GPT2LMHeadModel.from_pretrained("distilgpt2")
|
| 7 |
+
|
| 8 |
+
# Ensure the model doesn't generate any special tokens like <pad>
|
| 9 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 10 |
|
|
|
|
| 11 |
def chat(message, history):
|
| 12 |
+
# Prepare the conversation history
|
| 13 |
full_prompt = ""
|
| 14 |
for user, bot in history:
|
| 15 |
full_prompt += f"User: {user}\nBot: {bot}\n"
|
| 16 |
full_prompt += f"User: {message}\nBot:"
|
| 17 |
|
| 18 |
+
# Tokenize the input and generate a response
|
| 19 |
+
inputs = tokenizer(full_prompt, return_tensors="pt")
|
| 20 |
+
outputs = model.generate(inputs["input_ids"], max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
|
| 21 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 22 |
+
|
| 23 |
+
# Extract only the new reply
|
| 24 |
+
reply = reply.split("Bot:")[-1].strip()
|
| 25 |
+
|
| 26 |
return reply
|
| 27 |
|
| 28 |
+
# Set up the Gradio interface
|
| 29 |
+
gr.ChatInterface(fn=chat, title="Simple Chatbot with DistilGPT-2").launch()
|
|
|
|
|
|