zohoor1992 commited on
Commit
1897ac8
·
verified ·
1 Parent(s): d627016

Build AI Chatbots in Minutes

Browse files

Create fully functional AI chatbots

Files changed (1) hide show
  1. app.py +19 -0
app.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
2
+ import gradio as gr
3
+
4
+ tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-1B-distill")
5
+ model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-1B-distill")
6
+
7
+ conversation_history = []
8
+
9
+ def chat(user_input):
10
+ conversation_history.append("أنت: " + user_input)
11
+ context = "\n".join(conversation_history)
12
+ inputs = tokenizer([context], return_tensors="pt", truncation=True, max_length=512)
13
+ reply_ids = model.generate(**inputs, max_new_tokens=100)
14
+ reply_text = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
15
+ conversation_history.append("🤖: " + reply_text)
16
+ return reply_text
17
+
18
+ iface = gr.Interface(fn=chat, inputs="text", outputs="text", title="🤖 روبوت دردشة ذكي")
19
+ iface.launch()