UMAR798 commited on
Commit
38cc83d
Β·
verified Β·
1 Parent(s): 669a5af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +222 -49
app.py CHANGED
@@ -1,40 +1,143 @@
1
- # -------------------- UI --------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  css = """
4
  body{
5
- background: linear-gradient(135deg,#0f172a,#1e293b);
6
  }
7
 
8
  .gradio-container{
9
- max-width:1000px !important;
10
  margin:auto;
 
11
  }
12
 
13
- h1{
 
 
 
 
14
  text-align:center;
15
- font-size:42px;
16
  font-weight:700;
17
  color:white;
 
18
  }
19
 
20
- .subtitle{
21
  text-align:center;
22
  color:#cbd5e1;
23
  font-size:18px;
24
- margin-bottom:20px;
25
  }
26
 
27
- .chatbox{
28
- border-radius:20px;
 
 
29
  }
30
 
31
- footer{
32
- display:none;
33
  }
34
 
35
  button{
36
- border-radius:15px !important;
37
- font-size:18px !important;
38
  font-weight:bold !important;
39
  }
40
 
@@ -44,73 +147,143 @@ button{
44
  }
45
 
46
  #clear{
47
- background:#334155;
48
  color:white;
49
  }
50
 
51
- #title{
52
- text-align:center;
 
 
 
 
53
  }
54
  """
55
 
56
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), css=css) as demo:
 
 
 
 
 
 
57
 
58
  gr.Markdown(
59
- """
60
- # πŸ€– AI FAQ Chatbot
61
-
62
- <div class="subtitle">
63
- Ask any FAQ and receive the most relevant answer using NLP,
64
- TF-IDF and Cosine Similarity.
65
- </div>
66
- """
67
  )
68
 
69
  chatbot_ui = gr.Chatbot(
70
- height=500,
71
  bubble_full_width=False,
72
  show_copy_button=True,
73
- avatar_images=("https://cdn-icons-png.flaticon.com/512/847/847969.png",
74
- "https://cdn-icons-png.flaticon.com/512/4712/4712109.png")
75
  )
76
 
77
  with gr.Row():
78
 
79
  textbox = gr.Textbox(
80
- placeholder="πŸ’¬ Ask your question...",
81
- scale=8,
82
- container=False
83
  )
84
 
85
- send = gr.Button("Send πŸš€", elem_id="send", scale=1)
 
 
 
 
86
 
87
- clear = gr.Button("πŸ—‘ Clear Chat", elem_id="clear")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
 
90
  def respond(message, history):
91
 
 
 
 
92
  answer = chatbot(message)
93
 
94
- history.append((message, answer))
 
 
 
 
 
 
95
 
96
  return "", history
 
 
 
97
 
 
 
 
 
 
98
 
99
- send.click(
100
- respond,
101
- [textbox, chatbot_ui],
102
- [textbox, chatbot_ui]
103
- )
104
 
105
- textbox.submit(
106
- respond,
107
- [textbox, chatbot_ui],
108
- [textbox, chatbot_ui]
109
- )
110
 
111
- clear.click(
112
- lambda: [],
113
- outputs=chatbot_ui
114
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
- demo.launch()
 
 
1
+ import gradio as gr
2
+ import re
3
+ import nltk
4
+ from nltk.corpus import stopwords
5
+ from sklearn.feature_extraction.text import TfidfVectorizer
6
+ from sklearn.metrics.pairwise import cosine_similarity
7
+
8
+ # Download NLTK stopwords
9
+ nltk.download("stopwords")
10
+
11
+ # -------------------------------
12
+ # FAQ Dataset
13
+ # -------------------------------
14
+
15
+ faq_data = {
16
+ "What is Artificial Intelligence?": "Artificial Intelligence (AI) is the simulation of human intelligence by machines.",
17
+ "What is Machine Learning?": "Machine Learning is a subset of AI that enables computers to learn from data.",
18
+ "What is Deep Learning?": "Deep Learning is a branch of Machine Learning based on neural networks.",
19
+ "What is NLP?": "Natural Language Processing enables computers to understand human language.",
20
+ "What is Python?": "Python is a popular programming language used in AI, automation, and web development.",
21
+ "Who developed Python?": "Python was developed by Guido van Rossum.",
22
+ "What is Hugging Face?": "Hugging Face is a platform for sharing machine learning models and datasets.",
23
+ "What is Google Colab?": "Google Colab is a free cloud-based notebook environment for Python.",
24
+ "What is Gradio?": "Gradio is a Python library used to build interactive machine learning web applications.",
25
+ "What is Streamlit?": "Streamlit is a framework for creating web applications using Python.",
26
+ "What is Cosine Similarity?": "Cosine Similarity measures how similar two text vectors are.",
27
+ "Who is Donald Trump?": "Donald Trump is the 45th and 47th President of the United States.",
28
+ "Who is Elon Musk?": "Elon Musk is the CEO of Tesla and SpaceX.",
29
+ "What is ChatGPT?": "ChatGPT is an AI chatbot developed by OpenAI.",
30
+ "What is TensorFlow?": "TensorFlow is an open-source machine learning framework developed by Google.",
31
+ "What is Scikit-learn?": "Scikit-learn is a Python library for machine learning.",
32
+ "What is Data Science?": "Data Science is the field of extracting knowledge and insights from data.",
33
+ "What is Neural Network?": "A Neural Network is a machine learning model inspired by the human brain.",
34
+ "What is Computer Vision?": "Computer Vision enables computers to interpret and understand images and videos.",
35
+ "What is Generative AI?": "Generative AI creates new content such as text, images, and code."
36
+ }
37
+
38
+ # -------------------------------
39
+ # Text Preprocessing
40
+ # -------------------------------
41
+
42
+ stop_words = set(stopwords.words("english"))
43
+
44
+ def preprocess(text):
45
+ text = text.lower()
46
+ text = re.sub(r'[^a-zA-Z0-9 ]', '', text)
47
+ words = text.split()
48
+ words = [word for word in words if word not in stop_words]
49
+ return " ".join(words)
50
+
51
+ questions = list(faq_data.keys())
52
+ answers = list(faq_data.values())
53
+
54
+ processed_questions = [preprocess(q) for q in questions]
55
+
56
+ vectorizer = TfidfVectorizer()
57
+
58
+ question_vectors = vectorizer.fit_transform(processed_questions)
59
+
60
+ # -------------------------------
61
+ # Chatbot Function
62
+ # -------------------------------
63
+
64
+ def chatbot(message):
65
+
66
+ if not message.strip():
67
+ return "Please enter a question."
68
+
69
+ processed = preprocess(message)
70
+
71
+ user_vector = vectorizer.transform([processed])
72
+
73
+ similarity = cosine_similarity(user_vector, question_vectors)
74
+
75
+ index = similarity.argmax()
76
+
77
+ score = similarity[0][index]
78
+
79
+ if score < 0.25:
80
+ return (
81
+ "❌ Sorry, I couldn't find a relevant answer.\n\n"
82
+ "Try asking about:\n"
83
+ "β€’ AI\n"
84
+ "β€’ Machine Learning\n"
85
+ "β€’ Python\n"
86
+ "β€’ Hugging Face\n"
87
+ "β€’ Google Colab\n"
88
+ "β€’ NLP\n"
89
+ "β€’ Elon Musk\n"
90
+ "β€’ Donald Trump"
91
+ )
92
+
93
+ return answers[index]
94
+ # ==========================================
95
+ # Beautiful ChatGPT Style UI
96
+ # ==========================================
97
 
98
  css = """
99
  body{
100
+ background:linear-gradient(135deg,#0f172a,#1e293b);
101
  }
102
 
103
  .gradio-container{
104
+ max-width:1100px !important;
105
  margin:auto;
106
+ background:transparent;
107
  }
108
 
109
+ footer{
110
+ display:none;
111
+ }
112
+
113
+ #title{
114
  text-align:center;
115
+ font-size:40px;
116
  font-weight:700;
117
  color:white;
118
+ margin-top:10px;
119
  }
120
 
121
+ #subtitle{
122
  text-align:center;
123
  color:#cbd5e1;
124
  font-size:18px;
125
+ margin-bottom:25px;
126
  }
127
 
128
+ .gr-chatbot{
129
+ border-radius:20px !important;
130
+ border:2px solid #334155 !important;
131
+ background:#111827 !important;
132
  }
133
 
134
+ .gr-textbox textarea{
135
+ font-size:17px !important;
136
  }
137
 
138
  button{
139
+ border-radius:14px !important;
140
+ font-size:17px !important;
141
  font-weight:bold !important;
142
  }
143
 
 
147
  }
148
 
149
  #clear{
150
+ background:#ef4444;
151
  color:white;
152
  }
153
 
154
+ #examples{
155
+ padding-top:10px;
156
+ }
157
+
158
+ .example-btn{
159
+ border-radius:25px;
160
  }
161
  """
162
 
163
+ theme = gr.themes.Soft(
164
+ primary_hue="blue",
165
+ secondary_hue="violet",
166
+ neutral_hue="slate",
167
+ )
168
+
169
+ with gr.Blocks(theme=theme, css=css, title="AI FAQ Chatbot") as demo:
170
 
171
  gr.Markdown(
172
+ """
173
+ <div id="title">πŸ€– AI FAQ Chatbot</div>
174
+
175
+ <div id="subtitle">
176
+ Ask questions about AI, Python, Machine Learning, Hugging Face,
177
+ Google Colab and more.
178
+ </div>
179
+ """
180
  )
181
 
182
  chatbot_ui = gr.Chatbot(
183
+ height=520,
184
  bubble_full_width=False,
185
  show_copy_button=True,
186
+ label="Conversation"
 
187
  )
188
 
189
  with gr.Row():
190
 
191
  textbox = gr.Textbox(
192
+ placeholder="πŸ’¬ Type your question here...",
193
+ lines=2,
194
+ scale=8
195
  )
196
 
197
+ send_btn = gr.Button(
198
+ "πŸš€ Send",
199
+ elem_id="send",
200
+ scale=1
201
+ )
202
 
203
+ with gr.Row():
204
+
205
+ clear_btn = gr.Button(
206
+ "πŸ—‘ Clear Chat",
207
+ elem_id="clear"
208
+ )
209
+
210
+ gr.Examples(
211
+ examples=[
212
+ ["What is AI?"],
213
+ ["What is Machine Learning?"],
214
+ ["Who is Elon Musk?"],
215
+ ["Who is Donald Trump?"],
216
+ ["What is Hugging Face?"],
217
+ ["What is Google Colab?"],
218
+ ["What is Python?"],
219
+ ["What is NLP?"]
220
+ ],
221
+ inputs=textbox,
222
+ elem_id="examples"
223
+ )
224
 
225
 
226
  def respond(message, history):
227
 
228
+ if history is None:
229
+ history = []
230
+
231
  answer = chatbot(message)
232
 
233
+ history.append(
234
+ {"role":"user","content":message}
235
+ )
236
+
237
+ history.append(
238
+ {"role":"assistant","content":answer}
239
+ )
240
 
241
  return "", history
242
+ # ==========================================
243
+ # Event Handlers
244
+ # ==========================================
245
 
246
+ send_btn.click(
247
+ fn=respond,
248
+ inputs=[textbox, chatbot_ui],
249
+ outputs=[textbox, chatbot_ui]
250
+ )
251
 
252
+ textbox.submit(
253
+ fn=respond,
254
+ inputs=[textbox, chatbot_ui],
255
+ outputs=[textbox, chatbot_ui]
256
+ )
257
 
258
+ clear_btn.click(
259
+ lambda: (None, []),
260
+ outputs=[textbox, chatbot_ui]
261
+ )
 
262
 
263
+ # ==========================================
264
+ # Footer
265
+ # ==========================================
266
+
267
+ gr.Markdown(
268
+ """
269
+ ---
270
+ <center>
271
+
272
+ ### πŸš€ AI FAQ Chatbot
273
+
274
+ Built with ❀️ using
275
+
276
+ **Python β€’ Gradio β€’ NLP β€’ TF-IDF β€’ Scikit-learn β€’ NLTK**
277
+
278
+ Google Colab βœ”οΈ | Hugging Face Spaces βœ”οΈ
279
+
280
+ </center>
281
+ """
282
+ )
283
+
284
+ # ==========================================
285
+ # Launch App
286
+ # ==========================================
287
 
288
+ if __name__ == "__main__":
289
+ demo.launch()