Jitendra14355 commited on
Commit
0877c34
·
verified ·
1 Parent(s): c88793e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -11
app.py CHANGED
@@ -1,11 +1,14 @@
1
  # =========================================
2
- # FLAN-T5 Chatbot (100% Stable - FINAL)
3
  # =========================================
4
 
5
  import gradio as gr
6
  import torch
7
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
8
 
 
 
 
9
  MODEL_NAME = "google/flan-t5-base"
10
 
11
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
@@ -16,12 +19,15 @@ model = model.to(device)
16
 
17
 
18
  # -----------------------------
19
- # Chat Function (IMPORTANT)
20
  # -----------------------------
21
  def chat(message, history):
22
-
 
 
 
23
  prompt = f"""
24
- You are a helpful AI assistant.
25
  Answer clearly and naturally.
26
 
27
  User: {message}
@@ -46,16 +52,43 @@ Assistant:
46
 
47
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
48
 
49
- return response
 
 
 
 
 
 
 
 
 
 
 
50
 
51
 
52
  # -----------------------------
53
- # Gradio Chat Interface (🔥 FIX)
54
  # -----------------------------
55
- demo = gr.ChatInterface(
56
- fn=chat,
57
- title="🤖 AI Dialogue System (FLAN-T5)",
58
- description="Chat with AI using FLAN-T5"
59
- )
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  demo.launch()
 
1
  # =========================================
2
+ # FLAN-T5 Dialogue System (Gradio)
3
  # =========================================
4
 
5
  import gradio as gr
6
  import torch
7
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
8
 
9
+ # -----------------------------
10
+ # 1. Load Model
11
+ # -----------------------------
12
  MODEL_NAME = "google/flan-t5-base"
13
 
14
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
19
 
20
 
21
  # -----------------------------
22
+ # 2. Chat Function
23
  # -----------------------------
24
  def chat(message, history):
25
+ if history is None:
26
+ history = []
27
+
28
+ # 🔥 Better prompt for conversational response
29
  prompt = f"""
30
+ You are a helpful and friendly AI assistant.
31
  Answer clearly and naturally.
32
 
33
  User: {message}
 
52
 
53
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
54
 
55
+ # ✅ Gradio v4 format
56
+ history.append({"role": "user", "content": message})
57
+ history.append({"role": "assistant", "content": response})
58
+
59
+ return history, ""
60
+
61
+
62
+ # -----------------------------
63
+ # 3. Reset Function
64
+ # -----------------------------
65
+ def reset_chat():
66
+ return [], ""
67
 
68
 
69
  # -----------------------------
70
+ # 4. UI
71
  # -----------------------------
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown("## 🤖 AI Dialogue System (FLAN-T5)")
 
 
 
74
 
75
+ chatbot = gr.Chatbot(type="messages")
76
+
77
+ msg = gr.Textbox(
78
+ placeholder="Type your message...",
79
+ label="Your Message"
80
+ )
81
+
82
+ with gr.Row():
83
+ send = gr.Button("Send")
84
+ clear = gr.Button("Clear Chat")
85
+
86
+ send.click(chat, [msg, chatbot], [chatbot, msg])
87
+ msg.submit(chat, [msg, chatbot], [chatbot, msg])
88
+ clear.click(reset_chat, outputs=[chatbot, msg])
89
+
90
+
91
+ # -----------------------------
92
+ # 5. Launch
93
+ # -----------------------------
94
  demo.launch()