Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
| 6 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
| 7 |
+
|
| 8 |
+
chat_history_ids = None
|
| 9 |
+
|
| 10 |
+
def chat(user_input):
|
| 11 |
+
global chat_history_ids
|
| 12 |
+
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 13 |
+
|
| 14 |
+
bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1) if chat_history_ids is not None else new_input_ids
|
| 15 |
+
|
| 16 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
| 17 |
+
|
| 18 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 19 |
+
|
| 20 |
+
return response
|
| 21 |
+
|
| 22 |
+
gr.Interface(fn=chat, inputs="text", outputs="text", title="🤖 NetherMite Chatbot").launch()
|