Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("cedpsam/chatbot_fr")
|
8 |
+
|
9 |
+
model = AutoModelWithLMHead.from_pretrained("cedpsam/chatbot_fr")
|
10 |
+
global_history=[]
|
11 |
+
def chat(message, history):
|
12 |
+
|
13 |
+
history = history or global_history
|
14 |
+
prev="\n".join([""f"{a}\n{b}" for a ,b in global_history])
|
15 |
+
bot_input_ids=tokenizer.encode(f"{prev}\n {message}" + tokenizer.eos_token, return_tensors='pt')
|
16 |
+
chat_history_ids = model.generate(
|
17 |
+
bot_input_ids, max_length=80,
|
18 |
+
pad_token_id=tokenizer.eos_token_id,
|
19 |
+
top_p=0.92, top_k = 50,
|
20 |
+
num_beams =8,
|
21 |
+
max_time=20
|
22 |
+
)
|
23 |
+
response=tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
24 |
+
history.append((message, response))
|
25 |
+
|
26 |
+
return history, history
|
27 |
+
|
28 |
+
|
29 |
+
iface = gr.Interface(
|
30 |
+
chat,
|
31 |
+
["text", "state"],
|
32 |
+
["chatbot", "state"],
|
33 |
+
allow_screenshot=False,
|
34 |
+
allow_flagging="never",
|
35 |
+
)
|
36 |
+
iface.launch()
|