Spaces:
Sleeping
Sleeping
Commit ·
dfa4de0
1
Parent(s): 7dac2df
agregar app
Browse files- app.py +47 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Load model directly
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
""" from huggingface_hub import login
|
| 7 |
+
|
| 8 |
+
login(token="token", add_to_git_credential=True) """
|
| 9 |
+
|
| 10 |
+
model_name = "microsoft/DialoGPT-large"
|
| 11 |
+
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 14 |
+
|
| 15 |
+
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
| 16 |
+
|
| 17 |
+
chat_history_ids = None
|
| 18 |
+
|
| 19 |
+
def generate_response(user_input, history = []):
|
| 20 |
+
global chat_history_ids # Para modificar la variable global
|
| 21 |
+
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 22 |
+
|
| 23 |
+
if len(history) > 0:
|
| 24 |
+
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
|
| 25 |
+
else:
|
| 26 |
+
bot_input_ids = new_user_input_ids
|
| 27 |
+
|
| 28 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=10000, pad_token_id=tokenizer.eos_token_id)
|
| 29 |
+
|
| 30 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 31 |
+
|
| 32 |
+
# Para depuración
|
| 33 |
+
#print("user", new_user_input_ids)
|
| 34 |
+
#print("bot",chat_history_ids[:, bot_input_ids.shape[-1]:][0])
|
| 35 |
+
#print(chat_history_ids)
|
| 36 |
+
|
| 37 |
+
return response
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
chatbot_interface = gr.ChatInterface(fn=generate_response)
|
| 41 |
+
|
| 42 |
+
demo = gr.TabbedInterface([
|
| 43 |
+
chatbot_interface
|
| 44 |
+
], ["chatbot"])
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|
| 3 |
+
torch
|