Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,21 @@
|
|
|
|
|
| 1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
import torch
|
| 3 |
|
| 4 |
-
print("Simple AI Chatbot (type 'quit' to exit)")
|
| 5 |
-
|
| 6 |
# Load model
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
|
| 8 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
|
| 9 |
|
| 10 |
-
# Chat history
|
| 11 |
chat_history_ids = None
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
if user_input.lower() == "quit":
|
| 16 |
-
break
|
| 17 |
-
|
| 18 |
-
# Encode user input
|
| 19 |
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 20 |
-
|
| 21 |
-
# Append previous chat history if exists
|
| 22 |
bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1) if chat_history_ids is not None else new_input_ids
|
| 23 |
-
|
| 24 |
-
# Generate bot response
|
| 25 |
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
| 26 |
bot_response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
|
|
|
|
|
|
| 5 |
# Load model
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
|
| 7 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
|
| 8 |
|
|
|
|
| 9 |
chat_history_ids = None
|
| 10 |
|
| 11 |
+
def chat(user_input):
|
| 12 |
+
global chat_history_ids
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
|
|
|
|
|
|
| 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 |
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
| 16 |
bot_response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 17 |
+
return bot_response
|
| 18 |
|
| 19 |
+
iface = gr.Interface(fn=chat, inputs="text", outputs="text", title="Simple AI Chatbot")
|
| 20 |
+
iface.launch()
|
| 21 |
|