jsakshi commited on
Commit
cf18c3a
·
verified ·
1 Parent(s): cde2b90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -49
app.py CHANGED
@@ -88,13 +88,14 @@ HEADERS = {
88
  }
89
 
90
  def query_deepseek(prompt):
91
- """Send a prompt to DeepSeek-R1 and get a response."""
92
  payload = {
93
  "inputs": prompt,
94
  "parameters": {
95
- "max_new_tokens": 100, # Limit response length
96
- "temperature": 0.6, # Control creativity
97
- "top_p": 0.9 # Nucleus sampling
 
98
  }
99
  }
100
 
@@ -103,6 +104,9 @@ def query_deepseek(prompt):
103
  response.raise_for_status()
104
  result = response.json()
105
  generated_text = result[0]["generated_text"].strip()
 
 
 
106
  return generated_text
107
  except requests.exceptions.RequestException as e:
108
  return f"Error: Could not connect to the API - {str(e)}"
@@ -111,87 +115,120 @@ def query_deepseek(prompt):
111
 
112
  def chat_interface(user_input, history):
113
  """Handle chatbot interaction with Gradio."""
114
- # Construct a clean prompt
115
  if history:
116
- prompt = "\n".join([f"User: {h[0]}\nAssistant: {h[1]}" for h in history]) + f"\nUser: {user_input}\nAssistant: "
117
  else:
118
- prompt = f"User: {user_input}\nAssistant: "
119
 
120
- # Get the response
121
  response = query_deepseek(prompt)
122
 
123
- # Strip the prompt to avoid duplication
124
- if response.startswith(prompt):
125
- response = response[len(prompt):].strip()
126
 
127
- # Deduplicate sentences
128
  sentences = response.split(". ")
129
  unique_sentences = []
130
- [unique_sentences.append(s) for s in sentences if s not in unique_sentences]
131
- response = ". ".join(unique_sentences).strip()
132
-
133
- return response
134
 
135
- # Simplified black-and-white CSS with a blue send button
136
  css = """
137
  body {
138
- background: white; /* Plain white background */
139
- color: black; /* Black text */
140
- font-family: 'Arial', sans-serif;
141
  }
142
 
143
  .gr-chat-interface {
144
- background: white;
145
- color: black;
 
 
 
 
146
  }
147
 
148
- button#submit_btn { /* Target the send button specifically */
149
- background-color: #007bff !important; /* Blue send button */
150
- color: white !important;
151
- border: none !important;
152
- padding: 10px 20px !important;
153
- border-radius: 5px !important;
 
154
  }
155
 
156
- button#submit_btn:hover {
157
- background-color: #0056b3 !important; /* Darker blue on hover */
 
 
 
 
 
158
  }
159
 
160
- button { /* Other buttons remain black and white */
161
- background-color: white !important;
162
- color: black !important;
163
- border: 1px solid black !important;
164
- padding: 10px 20px !important;
165
- border-radius: 5px !important;
166
  }
167
 
168
- input, textarea {
169
- background: white !important;
170
- color: black !important;
171
- border: 1px solid black !important;
172
  border-radius: 5px !important;
173
  padding: 10px !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  }
175
 
176
  .title {
177
- color: black;
178
- font-size: 1.5em;
 
 
 
 
179
  }
180
 
181
  .description {
182
- color: black;
183
- font-style: italic;
 
 
 
 
 
 
 
184
  }
185
  """
186
 
187
- # Create Gradio interface
188
  interface = gr.ChatInterface(
189
  fn=chat_interface,
190
- title="LocoBot: DeepSeek-R1 Chatbot",
191
- description="Chat with DeepSeek-R1 powered by Hugging Face Inference API.",
192
- theme="default",
193
  css=css,
194
- submit_btn=gr.Button("Send", elem_id="submit_btn") # Custom send button with ID
 
195
  )
196
 
197
  if __name__ == "__main__":
 
88
  }
89
 
90
  def query_deepseek(prompt):
91
+ """Send a prompt to DeepSeek-R1 and get a clean response."""
92
  payload = {
93
  "inputs": prompt,
94
  "parameters": {
95
+ "max_new_tokens": 80, # Reduced to limit repetition
96
+ "temperature": 0.5, # Lowered for less randomness
97
+ "top_p": 0.85, # Tighter nucleus sampling
98
+ "stop": ["\n", "User:", "Assistant:"] # Stop at natural breaks
99
  }
100
  }
101
 
 
104
  response.raise_for_status()
105
  result = response.json()
106
  generated_text = result[0]["generated_text"].strip()
107
+ # Remove the prompt if it’s echoed back
108
+ if prompt in generated_text:
109
+ return generated_text.replace(prompt, "").strip()
110
  return generated_text
111
  except requests.exceptions.RequestException as e:
112
  return f"Error: Could not connect to the API - {str(e)}"
 
115
 
116
  def chat_interface(user_input, history):
117
  """Handle chatbot interaction with Gradio."""
118
+ # Build a concise prompt with history
119
  if history:
120
+ prompt = "\n".join([f"U: {h[0]} | A: {h[1]}" for h in history]) + f"\nU: {user_input} | A: "
121
  else:
122
+ prompt = f"U: {user_input} | A: "
123
 
124
+ # Get response and ensure no duplication
125
  response = query_deepseek(prompt)
126
 
127
+ # Clean up any lingering prompt markers
128
+ response = response.replace("U: ", "").replace("| A: ", "").strip()
 
129
 
130
+ # Final deduplication: split into sentences and keep unique ones
131
  sentences = response.split(". ")
132
  unique_sentences = []
133
+ [unique_sentences.append(s) for s in sentences if s not in unique_sentences and s]
134
+ return ". ".join(unique_sentences).strip()
 
 
135
 
136
+ # New interactive CSS with a futuristic, playful vibe
137
  css = """
138
  body {
139
+ background: #000000; /* Deep black background */
140
+ color: #00ffcc; /* Neon cyan text */
141
+ font-family: 'Courier New', monospace; /* Retro terminal feel */
142
  }
143
 
144
  .gr-chat-interface {
145
+ background: rgba(0, 0, 0, 0.8);
146
+ border: 2px solid #00ffcc;
147
+ border-radius: 10px;
148
+ padding: 20px;
149
+ max-width: 700px;
150
+ margin: 20px auto;
151
  }
152
 
153
+ .chatbot-container {
154
+ height: 400px;
155
+ overflow-y: auto;
156
+ background: #1a1a1a;
157
+ border-radius: 8px;
158
+ padding: 10px;
159
+ color: #00ffcc;
160
  }
161
 
162
+ /* Style the chat messages */
163
+ .chatbot-container .message {
164
+ margin: 10px 0;
165
+ padding: 8px;
166
+ border-radius: 5px;
167
+ background: #333333;
168
+ transition: transform 0.2s ease;
169
  }
170
 
171
+ .chatbot-container .message:hover {
172
+ transform: translateX(5px); /* Slight slide on hover */
 
 
 
 
173
  }
174
 
175
+ input {
176
+ background: #1a1a1a !important;
177
+ color: #00ffcc !important;
178
+ border: 1px solid #00ffcc !important;
179
  border-radius: 5px !important;
180
  padding: 10px !important;
181
+ font-family: 'Courier New', monospace;
182
+ }
183
+
184
+ button#submit_btn {
185
+ background: #ff007a !important; /* Neon pink send button */
186
+ color: #ffffff !important;
187
+ border: none !important;
188
+ padding: 10px 20px !important;
189
+ border-radius: 20px !important;
190
+ font-family: 'Courier New', monospace;
191
+ font-weight: bold;
192
+ box-shadow: 0 0 10px #ff007a;
193
+ transition: all 0.3s ease;
194
+ }
195
+
196
+ button#submit_btn:hover {
197
+ background: #cc0066 !important; /* Darker pink on hover */
198
+ box-shadow: 0 0 15px #ff007a;
199
+ transform: scale(1.1); /* Slight grow effect */
200
  }
201
 
202
  .title {
203
+ color: #ff007a;
204
+ font-size: 2em;
205
+ text-align: center;
206
+ text-transform: uppercase;
207
+ letter-spacing: 2px;
208
+ animation: glow 1.5s infinite alternate;
209
  }
210
 
211
  .description {
212
+ color: #00ffcc;
213
+ text-align: center;
214
+ font-size: 0.9em;
215
+ }
216
+
217
+ /* Glow animation for title */
218
+ @keyframes glow {
219
+ from { text-shadow: 0 0 5px #ff007a; }
220
+ to { text-shadow: 0 0 15px #ff007a, 0 0 25px #ff007a; }
221
  }
222
  """
223
 
224
+ # Create Gradio interface with custom chatbot
225
  interface = gr.ChatInterface(
226
  fn=chat_interface,
227
+ title="LocoBot: Neural Nexus",
228
+ description="A quirky AI companion powered by DeepSeek-R1. Ask away!",
 
229
  css=css,
230
+ submit_btn=gr.Button("Transmit", elem_id="submit_btn"),
231
+ textbox=gr.Textbox(placeholder="Type your query, human..."),
232
  )
233
 
234
  if __name__ == "__main__":