assemsabry commited on
Commit
5c55abc
·
verified ·
1 Parent(s): cf7a75c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -0
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
+
4
+ model_name = "microsoft/DialoGPT-medium" # ممكن تغيره حسب رغبتك
5
+
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name)
8
+
9
+ chat_history_ids = None
10
+
11
+ def chat_with_bot(user_input, history=[]):
12
+ global chat_history_ids
13
+ input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
14
+
15
+ if chat_history_ids is not None:
16
+ input_ids = torch.cat([chat_history_ids, input_ids], dim=-1)
17
+
18
+ output = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
19
+ chat_history_ids = output
20
+
21
+ response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
22
+ return response
23
+
24
+ iface = gr.Interface(fn=chat_with_bot, inputs="text", outputs="text", title="ChatBot 🤖")
25
+ iface.launch()