jsakshi commited on
Commit
c82ba5c
·
verified ·
1 Parent(s): a27780f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -51
app.py CHANGED
@@ -69,6 +69,7 @@ interface = gr.ChatInterface(
69
  if __name__ == "__main__":
70
  interface.launch(server_name="0.0.0.0", server_port=7860)'''
71
 
 
72
  import requests
73
  import os
74
  import gradio as gr
@@ -88,14 +89,13 @@ HEADERS = {
88
  }
89
 
90
  def query_deepseek(prompt):
91
- """Send a prompt to DeepSeek-R1 and get a clean response in English."""
92
  payload = {
93
  "inputs": prompt,
94
  "parameters": {
95
- "max_new_tokens": 80, # Limit response length
96
- "temperature": 0.5, # Less randomness
97
- "top_p": 0.85, # Focused sampling
98
- "stop": ["\n", "User:", "Assistant:"] # Stop at natural breaks
99
  }
100
  }
101
 
@@ -104,44 +104,38 @@ def query_deepseek(prompt):
104
  response.raise_for_status()
105
  result = response.json()
106
  generated_text = result[0]["generated_text"].strip()
107
- # Remove the prompt if 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)}"
113
  except (KeyError, IndexError):
114
  return "Error: Unexpected response format from API"
115
 
116
  def chat_interface(user_input, history):
117
  """Handle chatbot interaction with Gradio."""
118
- # Build a prompt with explicit English instruction
119
- if history:
120
- prompt = "\n".join([f"U: {h[0]} | A: {h[1]}" for h in history]) + f"\nU: {user_input} | A: Respond in English: "
121
- else:
122
- prompt = f"U: {user_input} | A: Respond in English: "
123
-
124
- # Get response and clean it up
125
  response = query_deepseek(prompt)
126
- response = response.replace("U: ", "").replace("| A: Respond in English: ", "").strip()
127
 
128
- # Deduplicate sentences
129
- sentences = response.split(". ")
130
- unique_sentences = []
131
- [unique_sentences.append(s) for s in sentences if s not in unique_sentences and s]
132
- return ". ".join(unique_sentences).strip()
133
 
134
- # Updated interactive CSS with a blue send button
135
  css = """
136
  body {
137
- background: #000000; /* Deep black background */
138
- color: #00ffcc; /* Neon cyan text */
139
- font-family: 'Courier New', monospace; /* Retro terminal feel */
140
  }
141
 
142
  .gr-chat-interface {
143
- background: rgba(0, 0, 0, 0.8);
144
- border: 2px solid #00ffcc;
145
  border-radius: 10px;
146
  padding: 20px;
147
  max-width: 700px;
@@ -154,7 +148,7 @@ body {
154
  background: #1a1a1a;
155
  border-radius: 8px;
156
  padding: 10px;
157
- color: #00ffcc;
158
  }
159
 
160
  .chatbot-container .message {
@@ -162,17 +156,12 @@ body {
162
  padding: 8px;
163
  border-radius: 5px;
164
  background: #333333;
165
- transition: transform 0.2s ease;
166
- }
167
-
168
- .chatbot-container .message:hover {
169
- transform: translateX(5px); /* Slight slide on hover */
170
  }
171
 
172
  input {
173
  background: #1a1a1a !important;
174
- color: #00ffcc !important;
175
- border: 1px solid #00ffcc !important;
176
  border-radius: 5px !important;
177
  padding: 10px !important;
178
  font-family: 'Courier New', monospace;
@@ -186,47 +175,41 @@ button#submit_btn {
186
  border-radius: 20px !important;
187
  font-family: 'Courier New', monospace;
188
  font-weight: bold;
189
- box-shadow: 0 0 10px #007bff; /* Blue glow */
190
  transition: all 0.3s ease;
191
  }
192
 
193
  button#submit_btn:hover {
194
  background: #0056b3 !important; /* Darker blue on hover */
195
  box-shadow: 0 0 15px #007bff;
196
- transform: scale(1.1); /* Slight grow effect */
197
  }
198
 
199
  .title {
200
- color: #00ffcc; /* Changed to cyan for consistency */
201
  font-size: 2em;
202
  text-align: center;
203
  text-transform: uppercase;
204
  letter-spacing: 2px;
205
- animation: glow 1.5s infinite alternate;
206
  }
207
 
208
  .description {
209
- color: #00ffcc;
210
  text-align: center;
211
  font-size: 0.9em;
212
  }
213
-
214
- /* Glow animation for title */
215
- @keyframes glow {
216
- from { text-shadow: 0 0 5px #00ffcc; }
217
- to { text-shadow: 0 0 15px #00ffcc, 0 0 25px #00ffcc; }
218
- }
219
  """
220
 
221
  # Create Gradio interface
222
  interface = gr.ChatInterface(
223
  fn=chat_interface,
224
- title="LocoBot: Neural Nexus",
225
- description="A quirky AI companion powered by DeepSeek-R1. Ask away!",
226
  css=css,
227
  submit_btn=gr.Button("Send", elem_id="submit_btn"), # Blue send button
228
- textbox=gr.Textbox(placeholder="Type your query, human..."),
229
  )
230
 
231
  if __name__ == "__main__":
232
- interface.launch(server_name="0.0.0.0", server_port=7860)
 
 
69
  if __name__ == "__main__":
70
  interface.launch(server_name="0.0.0.0", server_port=7860)'''
71
 
72
+
73
  import requests
74
  import os
75
  import gradio as gr
 
89
  }
90
 
91
  def query_deepseek(prompt):
92
+ """Send a prompt to DeepSeek-R1 and get a response in English."""
93
  payload = {
94
  "inputs": prompt,
95
  "parameters": {
96
+ "max_new_tokens": 100, # Slightly increased for flexibility
97
+ "temperature": 0.7, # Balanced creativity
98
+ "top_p": 0.9 # Relaxed sampling
 
99
  }
100
  }
101
 
 
104
  response.raise_for_status()
105
  result = response.json()
106
  generated_text = result[0]["generated_text"].strip()
107
+ print(f"Raw API response: {generated_text}") # Debug output
108
+ # Remove the prompt if included
109
+ if generated_text.startswith(prompt):
110
+ return generated_text[len(prompt):].strip()
111
  return generated_text
112
  except requests.exceptions.RequestException as e:
113
+ return f"Error: API request failed - {str(e)}"
114
  except (KeyError, IndexError):
115
  return "Error: Unexpected response format from API"
116
 
117
  def chat_interface(user_input, history):
118
  """Handle chatbot interaction with Gradio."""
119
+ # Simple prompt with English instruction
120
+ prompt = f"User: {user_input}\nAssistant: Respond in English: "
 
 
 
 
 
121
  response = query_deepseek(prompt)
 
122
 
123
+ # Clean up the response
124
+ response = response.replace("Assistant: Respond in English: ", "").strip()
125
+ print(f"Final response: {response}") # Debug output
126
+ return response if response else "Sorry, I couldn’t generate a response. Try again!"
 
127
 
128
+ # Simplified interactive CSS with a blue send button
129
  css = """
130
  body {
131
+ background: #000000; /* Black background */
132
+ color: #ffffff; /* White text for readability */
133
+ font-family: 'Courier New', monospace;
134
  }
135
 
136
  .gr-chat-interface {
137
+ background: rgba(0, 0, 0, 0.9);
138
+ border: 1px solid #ffffff;
139
  border-radius: 10px;
140
  padding: 20px;
141
  max-width: 700px;
 
148
  background: #1a1a1a;
149
  border-radius: 8px;
150
  padding: 10px;
151
+ color: #ffffff;
152
  }
153
 
154
  .chatbot-container .message {
 
156
  padding: 8px;
157
  border-radius: 5px;
158
  background: #333333;
 
 
 
 
 
159
  }
160
 
161
  input {
162
  background: #1a1a1a !important;
163
+ color: #ffffff !important;
164
+ border: 1px solid #ffffff !important;
165
  border-radius: 5px !important;
166
  padding: 10px !important;
167
  font-family: 'Courier New', monospace;
 
175
  border-radius: 20px !important;
176
  font-family: 'Courier New', monospace;
177
  font-weight: bold;
178
+ box-shadow: 0 0 10px #007bff;
179
  transition: all 0.3s ease;
180
  }
181
 
182
  button#submit_btn:hover {
183
  background: #0056b3 !important; /* Darker blue on hover */
184
  box-shadow: 0 0 15px #007bff;
185
+ transform: scale(1.1);
186
  }
187
 
188
  .title {
189
+ color: #007bff; /* Blue title to match button */
190
  font-size: 2em;
191
  text-align: center;
192
  text-transform: uppercase;
193
  letter-spacing: 2px;
 
194
  }
195
 
196
  .description {
197
+ color: #ffffff;
198
  text-align: center;
199
  font-size: 0.9em;
200
  }
 
 
 
 
 
 
201
  """
202
 
203
  # Create Gradio interface
204
  interface = gr.ChatInterface(
205
  fn=chat_interface,
206
+ title="LocoBot: Core Unit",
207
+ description="A straightforward AI powered by DeepSeek-R1. Ask me anything!",
208
  css=css,
209
  submit_btn=gr.Button("Send", elem_id="submit_btn"), # Blue send button
210
+ textbox=gr.Textbox(placeholder="Enter your question..."),
211
  )
212
 
213
  if __name__ == "__main__":
214
+ interface.launch(server_name="0.0.0.0", server_port=7860)
215
+