WSLINMSAI commited on
Commit
961070f
·
verified ·
1 Parent(s): b2693c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -33
app.py CHANGED
@@ -5,65 +5,195 @@ import functools
5
  # Cache the pipeline so it loads only once
6
  @functools.lru_cache(maxsize=1)
7
  def load_pipeline():
8
- return pipeline("text-generation", model="EleutherAI/gpt-neo-125M", device=-1)
 
9
 
10
  def dental_chatbot_response(message, history):
11
  """
12
- Generates responses focused on dental terminology.
13
  """
14
  prompt = (
15
  "You are a highly knowledgeable and friendly dental expert chatbot. "
16
  "Provide detailed and accurate explanations of dental terms, procedures, and treatments. "
17
- "If the query is not dental-related, respond helpfully and informatively.\n\n"
18
- f"User: {message}\n\nChatbot:"
19
  )
20
-
21
  gen_pipe = load_pipeline()
22
  generated = gen_pipe(
23
  prompt,
24
- max_length=200, # Adjust for desired detail vs. speed
25
- num_return_sequences=1,
26
  do_sample=True,
27
- top_p=0.9, # Nucleus sampling for diverse outputs
28
- top_k=50 # Top-k sampling for quality control
29
  )
30
-
31
- generated_text = generated[0]["generated_text"]
32
- # Safely extract the chatbot's response
33
- if "Chatbot:" in generated_text:
34
- ai_response = generated_text.split("Chatbot:")[1].strip()
35
- else:
36
- ai_response = generated_text.strip()
37
  return ai_response
38
 
39
- # Custom CSS to enhance the interface look
40
  custom_css = """
 
41
  body {
42
- background: #f8f9fa;
43
  font-family: Arial, sans-serif;
 
 
44
  }
45
- .gradio-container {
 
 
46
  background: #ffffff;
47
- border-radius: 10px;
48
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
49
- margin: 20px;
 
50
  padding: 20px;
51
  }
52
- .gradio-title, .gradio-description {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  text-align: center;
54
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  """
56
 
57
- # Define the Gradio ChatInterface with the custom styling
58
- demo = gr.ChatInterface(
59
- fn=dental_chatbot_response,
60
- title="Advanced Dental Terminology Chatbot",
61
- description=(
62
- "Ask me anything about dental terms, procedures, and treatments! "
63
- "This chatbot is powered by an advanced LLM for detailed and accurate answers."
64
- ),
65
- css=custom_css
66
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  if __name__ == "__main__":
69
  demo.launch()
 
5
  # Cache the pipeline so it loads only once
6
  @functools.lru_cache(maxsize=1)
7
  def load_pipeline():
8
+ # Using google/flan-t5-base which is instruction-tuned for improved responses.
9
+ return pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
10
 
11
  def dental_chatbot_response(message, history):
12
  """
13
+ Generates responses with dental expertise using google/flan-t5-base.
14
  """
15
  prompt = (
16
  "You are a highly knowledgeable and friendly dental expert chatbot. "
17
  "Provide detailed and accurate explanations of dental terms, procedures, and treatments. "
18
+ "If the query is not dental-related, respond helpfully and informatively. "
19
+ f"User: {message}"
20
  )
21
+
22
  gen_pipe = load_pipeline()
23
  generated = gen_pipe(
24
  prompt,
25
+ max_length=200,
 
26
  do_sample=True,
27
+ top_p=0.9,
28
+ top_k=50,
29
  )
30
+ ai_response = generated[0]["generated_text"].strip()
 
 
 
 
 
 
31
  return ai_response
32
 
33
+ # Custom CSS for a colorful, modern look with icons and visual effects.
34
  custom_css = """
35
+ /* Overall background with a pleasant gradient */
36
  body {
37
+ background: linear-gradient(135deg, #ff9a9e, #fad0c4);
38
  font-family: Arial, sans-serif;
39
+ margin: 0;
40
+ padding: 0;
41
  }
42
+
43
+ /* Main container styling */
44
+ .container {
45
  background: #ffffff;
46
+ border-radius: 20px;
47
+ box-shadow: 0 8px 16px rgba(0,0,0,0.2);
48
+ max-width: 800px;
49
+ margin: 40px auto;
50
  padding: 20px;
51
  }
52
+
53
+ /* Header styling */
54
+ .header {
55
+ text-align: center;
56
+ margin-bottom: 20px;
57
+ }
58
+ .header h1 {
59
+ font-size: 2.5rem;
60
+ margin: 0;
61
+ color: #d81b60;
62
+ }
63
+ .header p {
64
+ font-size: 1.2rem;
65
+ color: #555555;
66
+ }
67
+
68
+ /* Chat message styling */
69
+ .chat-msg {
70
+ display: flex;
71
+ margin: 10px 0;
72
+ align-items: flex-start;
73
+ }
74
+ .chat-msg.user {
75
+ justify-content: flex-start;
76
+ }
77
+ .chat-msg.bot {
78
+ justify-content: flex-end;
79
+ }
80
+ .msg-content {
81
+ max-width: 70%;
82
+ padding: 10px 15px;
83
+ border-radius: 15px;
84
+ position: relative;
85
+ font-size: 1rem;
86
+ line-height: 1.3;
87
+ }
88
+ /* User message bubble */
89
+ .chat-msg.user .msg-content {
90
+ background: #ffe0b2;
91
+ color: #5d4037;
92
+ margin-left: 40px;
93
+ }
94
+ /* Bot message bubble */
95
+ .chat-msg.bot .msg-content {
96
+ background: #c5e1a5;
97
+ color: #33691e;
98
+ margin-right: 40px;
99
+ }
100
+
101
+ /* Icon styling for messages (using emojis) */
102
+ .msg-icon {
103
+ font-size: 1.5rem;
104
+ width: 30px;
105
  text-align: center;
106
  }
107
+ .chat-msg.user .msg-icon {
108
+ color: #d81b60;
109
+ }
110
+ .chat-msg.bot .msg-icon {
111
+ color: #2e7d32;
112
+ }
113
+
114
+ /* Input area styling */
115
+ #input-area {
116
+ display: flex;
117
+ margin-top: 20px;
118
+ }
119
+ #input-area textarea {
120
+ flex: 1;
121
+ padding: 10px;
122
+ font-size: 1rem;
123
+ border: 2px solid #ffe0b2;
124
+ border-radius: 10px;
125
+ outline: none;
126
+ }
127
+ #input-area button {
128
+ background: #d81b60;
129
+ color: #fff;
130
+ border: none;
131
+ padding: 0 20px;
132
+ margin-left: 10px;
133
+ border-radius: 10px;
134
+ cursor: pointer;
135
+ transition: background 0.3s ease;
136
+ font-size: 1rem;
137
+ }
138
+ #input-area button:hover {
139
+ background: #ff5252;
140
+ }
141
  """
142
 
143
+ # Build the custom Blocks interface
144
+ with gr.Blocks(css=custom_css) as demo:
145
+ with gr.Container(elem_classes=["container"]):
146
+ # Header with a dental icon in the title
147
+ with gr.Column(elem_classes=["header"]):
148
+ gr.Markdown("## 🦷 Advanced Dental Terminology Chatbot")
149
+ gr.Markdown("Ask me anything about dental terms, procedures, and treatments! This chatbot is powered by an instruction-tuned LLM for accurate and detailed answers.")
150
+
151
+ # Chat display area
152
+ chatbot = gr.HTML("<div id='chat-box'></div>")
153
+ chat_state = gr.State([]) # To store chat history as a list of (user, bot) pairs
154
+
155
+ # Input area
156
+ with gr.Row(elem_id="input-area"):
157
+ user_input = gr.Textbox(show_label=False, placeholder="Type your message here...", lines=2)
158
+ send_btn = gr.Button("Send")
159
+
160
+ def update_chat(user_message, history):
161
+ # Append the user's message to the chat history.
162
+ history = history + [(user_message, None)]
163
+ # Generate bot response.
164
+ bot_reply = dental_chatbot_response(user_message, history)
165
+ history[-1] = (user_message, bot_reply)
166
+ # Build HTML for the updated chat box with icons.
167
+ chat_html = ""
168
+ for user_msg, bot_msg in history:
169
+ # User message bubble with a smiley icon.
170
+ chat_html += f"""
171
+ <div class='chat-msg user'>
172
+ <div class='msg-icon'>🙂</div>
173
+ <div class='msg-content'>{user_msg}</div>
174
+ </div>
175
+ """
176
+ if bot_msg:
177
+ # Bot message bubble with a dental icon.
178
+ chat_html += f"""
179
+ <div class='chat-msg bot'>
180
+ <div class='msg-content'>{bot_msg}</div>
181
+ <div class='msg-icon'>🦷</div>
182
+ </div>
183
+ """
184
+ return chat_html, history, ""
185
+
186
+ # Wire up interactions: when the textbox is submitted or the send button is clicked.
187
+ user_input.submit(
188
+ update_chat,
189
+ inputs=[user_input, chat_state],
190
+ outputs=[chatbot, chat_state, user_input]
191
+ )
192
+ send_btn.click(
193
+ update_chat,
194
+ inputs=[user_input, chat_state],
195
+ outputs=[chatbot, chat_state, user_input]
196
+ )
197
 
198
  if __name__ == "__main__":
199
  demo.launch()