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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -99
app.py CHANGED
@@ -1,128 +1,116 @@
1
- import re
2
- import nltk
3
- import gradio as gr
4
 
5
- from nltk.corpus import stopwords
6
- from sklearn.feature_extraction.text import TfidfVectorizer
7
- from sklearn.metrics.pairwise import cosine_similarity
8
-
9
- # Download only stopwords (no punkt needed)
10
- nltk.download("stopwords")
11
-
12
- # ---------------------------------
13
- # FAQ Dataset
14
- # ---------------------------------
15
-
16
- faq_data = {
17
- "What is Artificial Intelligence?":
18
- "Artificial Intelligence (AI) is the simulation of human intelligence by machines.",
19
-
20
- "What is Machine Learning?":
21
- "Machine Learning is a subset of AI that enables computers to learn from data.",
22
-
23
- "What is Deep Learning?":
24
- "Deep Learning is a branch of Machine Learning based on neural networks.",
25
-
26
- "What is NLP?":
27
- "Natural Language Processing enables computers to understand human language.",
28
-
29
- "What is Python?":
30
- "Python is a popular programming language used in AI, web development, and automation.",
31
-
32
- "Who developed Python?":
33
- "Python was created by Guido van Rossum.",
34
-
35
- "What is Google Colab?":
36
- "Google Colab is a free cloud-based Jupyter Notebook for Python programming.",
37
-
38
- "What is Hugging Face?":
39
- "Hugging Face is a platform for Machine Learning models and datasets.",
40
-
41
- "What is Gradio?":
42
- "Gradio is a Python library for building interactive web interfaces.",
43
-
44
- "What is cosine similarity?":
45
- "Cosine similarity measures how similar two pieces of text are.",
46
 
47
- "Who is Donald Trump?":
48
- "Donald Trump is the 45th and 47th President of the United States.",
 
 
49
 
50
- "Who is Elon Musk?":
51
- "Elon Musk is the CEO of Tesla and SpaceX."
 
 
 
52
  }
53
 
54
- # ---------------------------------
55
- # Text Preprocessing
56
- # ---------------------------------
 
 
 
57
 
58
- stop_words = set(stopwords.words("english"))
 
 
59
 
60
- def preprocess(text):
61
- text = text.lower()
 
62
 
63
- # Remove punctuation
64
- text = re.sub(r'[^a-zA-Z0-9 ]', '', text)
 
 
 
65
 
66
- # Simple tokenization
67
- tokens = text.split()
 
 
68
 
69
- # Remove stopwords
70
- tokens = [word for word in tokens if word not in stop_words]
 
 
71
 
72
- return " ".join(tokens)
 
 
 
73
 
74
- questions = list(faq_data.keys())
75
- answers = list(faq_data.values())
76
 
77
- processed_questions = [preprocess(q) for q in questions]
 
 
78
 
79
- # ---------------------------------
80
- # TF-IDF
81
- # ---------------------------------
 
 
 
82
 
83
- vectorizer = TfidfVectorizer()
 
 
 
 
 
 
84
 
85
- question_vectors = vectorizer.fit_transform(processed_questions)
86
 
87
- # ---------------------------------
88
- # Chatbot Function
89
- # ---------------------------------
 
 
90
 
91
- def chatbot(user_input):
92
 
93
- if not user_input.strip():
94
- return "Please enter a question."
95
 
96
- processed_input = preprocess(user_input)
97
 
98
- input_vector = vectorizer.transform([processed_input])
99
 
100
- similarity = cosine_similarity(input_vector, question_vectors)
101
 
102
- best_match = similarity.argmax()
103
 
104
- score = similarity[0][best_match]
105
 
106
- if score < 0.25:
107
- return "Sorry, I couldn't find a relevant answer."
108
 
109
- return answers[best_match]
 
 
 
 
110
 
111
- # ---------------------------------
112
- # Gradio UI
113
- # ---------------------------------
 
 
114
 
115
- demo = gr.Interface(
116
- fn=chatbot,
117
- inputs=gr.Textbox(
118
- lines=2,
119
- placeholder="Ask your question here..."
120
- ),
121
- outputs=gr.Textbox(),
122
- title="πŸ€– FAQ Chatbot",
123
- description="FAQ Chatbot using NLP, TF-IDF, and Cosine Similarity",
124
- theme=gr.themes.Soft()
125
- )
126
 
127
- if __name__ == "__main__":
128
- demo.launch()
 
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
 
41
+ #send{
42
+ background:linear-gradient(90deg,#2563eb,#7c3aed);
43
+ color:white;
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()