kothapallimahitha commited on
Commit
9f7694a
·
verified ·
1 Parent(s): e8c007b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import BlenderbotSmallTokenizer, BlenderbotSmallForConditionalGeneration
3
+
4
+ # --- Load model and tokenizer ---
5
+ model_name = "facebook/blenderbot-90M"
6
+ tokenizer = BlenderbotSmallTokenizer.from_pretrained(model_name)
7
+ model = BlenderbotSmallForConditionalGeneration.from_pretrained(model_name)
8
+
9
+ # --- Define chatbot logic ---
10
+ def chat_with_bot(message, history):
11
+ if not message:
12
+ return "Hi there! 👋 Ask me something to get started."
13
+
14
+ # Rebuild conversation context
15
+ conversation = ""
16
+ if history:
17
+ for turn in history:
18
+ role = turn.get("role")
19
+ content = turn.get("content")
20
+ if role == "user":
21
+ conversation += f"User: {content}\n"
22
+ elif role == "assistant":
23
+ conversation += f"Bot: {content}\n"
24
+
25
+ conversation += f"User: {message}\nBot:"
26
+
27
+ # Tokenize input and generate reply
28
+ inputs = tokenizer(
29
+ conversation,
30
+ return_tensors="pt",
31
+ truncation=True,
32
+ padding="max_length",
33
+ max_length=512,
34
+ )
35
+ reply_ids = model.generate(**inputs, max_length=120)
36
+ reply = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
37
+ return reply
38
+
39
+ # --- Define welcome message ---
40
+ initial_message = [
41
+ {"role": "assistant", "content": "👋 Hello! I’m your chatbot. Ask me anything to start our conversation!"}
42
+ ]
43
+
44
+ # --- Create Gradio Interface ---
45
+ demo = gr.ChatInterface(
46
+ fn=chat_with_bot,
47
+ title="🤖 Mini Chatbot (Facebook BlenderBot-90M)",
48
+ description="Hi 👋 I’m a small conversational chatbot powered by Facebook’s BlenderBot-90M.",
49
+ theme="soft",
50
+ type="messages",
51
+ examples=["Hello!", "Tell me a fun fact", "How are you today?"],
52
+ )
53
+
54
+ # --- Launch App ---
55
+ if __name__ == "__main__":
56
+ demo.launch(share=True)