mroccuper commited on
Commit
8c34ed3
·
verified ·
1 Parent(s): 92e5dfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -28
app.py CHANGED
@@ -16,11 +16,6 @@ except ImportError:
16
  GENAI_AVAILABLE = False
17
  print("Warning: google-generativeai not installed, will attempt on-demand import")
18
 
19
- try:
20
- import pyperclip
21
- except ImportError:
22
- pyperclip = None
23
-
24
  # --- Environment Configuration ---
25
  GEMINI_KEY = os.environ.get("GEMINI_KEY", "")
26
  DEFAULT_PORT = int(os.environ.get("PORT", 7860))
@@ -253,31 +248,60 @@ def build_interface():
253
  api_name="generate"
254
  )
255
 
256
- # Implement browser-based clipboard functionality instead of pyperclip
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  copy_btn.click(
258
- None, # No Python function needed
259
  inputs=prompt_output,
260
- outputs=None,
261
- _js="""
262
- (text) => {
263
- if (!text) return;
264
- navigator.clipboard.writeText(text)
265
- .then(() => {
266
- // Show temporary success message
267
- const button = document.querySelector("button:contains('Copy')");
268
- const originalText = button.innerText;
269
- button.innerText = "✓ Copied!";
270
- setTimeout(() => {
271
- button.innerText = originalText;
272
- }, 2000);
273
- })
274
- .catch(err => {
275
- console.error('Failed to copy: ', err);
276
- alert('Copy failed. Please select and copy manually.');
277
- });
278
- return;
279
- }
280
- """
281
  )
282
 
283
  return app
 
16
  GENAI_AVAILABLE = False
17
  print("Warning: google-generativeai not installed, will attempt on-demand import")
18
 
 
 
 
 
 
19
  # --- Environment Configuration ---
20
  GEMINI_KEY = os.environ.get("GEMINI_KEY", "")
21
  DEFAULT_PORT = int(os.environ.get("PORT", 7860))
 
248
  api_name="generate"
249
  )
250
 
251
+ # Add a JavaScript function for clipboard functionality
252
+ app.load(None, None, None, _js="""
253
+ function setupCopyButton() {
254
+ // Find the copy button by its text
255
+ const copyButtons = Array.from(document.querySelectorAll('button')).filter(
256
+ button => button.textContent.includes('Copy')
257
+ );
258
+
259
+ if (copyButtons.length > 0) {
260
+ const copyBtn = copyButtons[0];
261
+ copyBtn.addEventListener('click', function() {
262
+ // Find the prompt output textarea
263
+ const textareas = document.querySelectorAll('textarea');
264
+ let promptTextarea = null;
265
+
266
+ for (let textarea of textareas) {
267
+ if (textarea.closest('div').querySelector('label')?.textContent.includes('Optimized Prompt')) {
268
+ promptTextarea = textarea;
269
+ break;
270
+ }
271
+ }
272
+
273
+ if (promptTextarea && promptTextarea.value) {
274
+ navigator.clipboard.writeText(promptTextarea.value)
275
+ .then(() => {
276
+ const originalText = copyBtn.textContent;
277
+ copyBtn.textContent = "✓ Copied!";
278
+ setTimeout(() => {
279
+ copyBtn.textContent = originalText;
280
+ }, 2000);
281
+ })
282
+ .catch(err => {
283
+ console.error('Failed to copy: ', err);
284
+ alert('Copy failed. Please select and copy manually.');
285
+ });
286
+ }
287
+ });
288
+ }
289
+ }
290
+
291
+ // Run our setup once the DOM is fully loaded
292
+ if (document.readyState === 'loading') {
293
+ document.addEventListener('DOMContentLoaded', setupCopyButton);
294
+ } else {
295
+ // DOM already loaded, run setup
296
+ setTimeout(setupCopyButton, 1000);
297
+ }
298
+ """)
299
+
300
+ # Add a simple no-op function for the copy button
301
  copy_btn.click(
302
+ lambda x: x,
303
  inputs=prompt_output,
304
+ outputs=prompt_output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  )
306
 
307
  return app