re-type commited on
Commit
e76415f
·
verified ·
1 Parent(s): ab77d45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +195 -189
app.py CHANGED
@@ -220,217 +220,223 @@ def get_sequence_stats(sequence):
220
 
221
  return stats
222
 
223
- # Initialize model on startup
224
- print("Initializing model...")
225
- model_status = initialize_model()
 
 
226
 
227
- # Create custom CSS for better styling
228
- custom_css = """
229
- .gene-app {
230
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
231
- }
232
 
233
- .status-ready {
234
- background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
235
- border: 2px solid #28a745;
236
- border-radius: 10px;
237
- padding: 15px;
238
- color: #155724;
239
- font-weight: bold;
240
- box-shadow: 0 2px 10px rgba(40, 167, 69, 0.2);
241
- }
242
 
243
- .status-error {
244
- background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
245
- border: 2px solid #dc3545;
246
- border-radius: 10px;
247
- padding: 15px;
248
- color: #721c24;
249
- font-weight: bold;
250
- box-shadow: 0 2px 10px rgba(220, 53, 69, 0.2);
251
- }
252
 
253
- .main-title {
254
- text-align: center;
255
- background: linear-gradient(135deg, #2E8B57 0%, #20B2AA 100%);
256
- -webkit-background-clip: text;
257
- -webkit-text-fill-color: transparent;
258
- background-clip: text;
259
- font-size: 2.5rem;
260
- font-weight: bold;
261
- margin-bottom: 1rem;
262
- }
263
 
264
- .instructions {
265
- background: #f8f9fa;
266
- border-radius: 10px;
267
- padding: 20px;
268
- border-left: 4px solid #2E8B57;
269
- margin: 1rem 0;
270
- }
271
 
272
- .sequence-stats {
273
- font-size: 0.9rem;
274
- color: #6c757d;
275
- font-style: italic;
276
- margin-top: 5px;
277
- }
278
- """
279
 
280
- # Create the interface
281
- print("Creating Gradio interface...")
282
 
283
- # Determine status message and styling
284
- if model_loaded:
285
- status_html = '''
286
- <div class="status-ready">
287
- <strong>✅ Model Status:</strong> Ready for gene prediction!<br>
288
- <small>🔬 Boundary-aware deep learning model loaded successfully</small>
289
- </div>
290
- '''
291
- else:
292
- status_html = f'''
293
- <div class="status-error">
294
- <strong>❌ Model Status:</strong> Model initialization failed<br>
295
- <small>📋 Details: {error_message}</small>
296
- </div>
297
- '''
298
 
299
- # Example sequences
300
- examples = [
301
- # Short example with clear gene
302
- ["ATGAAACGCATTAGCACCACCATTACCACCACCATCACCATTACCACAGGTAACGGTGCGGGCTGACGCGTACAGGAAACACAGAAAAAAGCCCGCACCTGACAGTGCGGGCTTTTTTTTTCGACCAAAGGTAACGAGGTAACAACCATGCGAGTGTTGAAGTTCGGCGGTACATCAGTGGCAAATGCAGAACGTTTTCTGCGTAA"],
303
- # Longer example
304
- ["ATGAAACGCATTAGCACCACCATTACCACCACCATCACCATTACCACAGGTAACGGTGCGGGCTGACGCGTACAGGAAACACAGAAAAAAGCCCGCACCTGACAGTGCGGGCTTTTTTTTTCGACCAAAGGTAACGAGGTAACAACCATGCGAGTGTTGAAGTTCGGCGGTACATCAGTGGCAAATGCAGAACGTTTTCTGCGTGTTGCCGATATTCTGGAAAGCAATGCCAGGCAGGGGCAGGTGGCCACCGTCCTCTCTGCCCCCGCCAAAATCACCAACCACCTGGTGGCGATGATTGAAAAAACCATTAGCGGCCAGGATGCTTTACCCAATATCAGCGATGCCGAACGTATTTTTGCCGAACTTTTGACGGGACTCGCCGCCGCCCAGCCGGGGTTCCCGCTGGCGCAATTGAAAACTTTCGTCGATCAGGAATTTGCCCAATAG"],
305
- ]
306
 
307
- # Create the interface with custom theme
308
- with gr.Blocks(
309
- title="🧬 Gene Prediction Tool",
310
- theme=gr.themes.Soft(primary_hue="emerald", secondary_hue="teal"),
311
- css=custom_css,
312
- head="<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
313
- ) as demo:
314
-
315
- gr.HTML('<h1 class="main-title">🧬 Advanced Gene Prediction Tool</h1>')
316
- gr.HTML('<p style="text-align: center; font-size: 1.1rem; color: #6c757d; margin-bottom: 2rem;">AI-powered boundary-aware gene detection system</p>')
317
-
318
- gr.HTML(status_html)
319
-
320
- with gr.Row():
321
- gr.HTML('''
322
- <div class="instructions">
323
- <h3>🔬 How to Use:</h3>
324
- <ol>
325
- <li><strong>Enter DNA sequence:</strong> Paste your sequence using A, T, C, G, N characters</li>
326
- <li><strong>Click Analyze:</strong> The AI model will predict gene regions</li>
327
- <li><strong>Review results:</strong> View detected genes with positions, codons, and confidence</li>
328
- </ol>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329
 
330
- <h4>📏 Requirements:</h4>
331
- <ul>
332
- <li>Characters: Only A, T, C, G, N allowed</li>
333
- <li>Length: 3 - 10,000 nucleotides</li>
334
- <li>Format: Raw sequence (FASTA headers will be ignored)</li>
335
- </ul>
 
 
 
 
 
 
 
 
336
  </div>
337
  ''')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338
 
339
- with gr.Row():
340
- with gr.Column(scale=1):
341
- sequence_input = gr.Textbox(
342
- label="🧬 DNA Sequence Input",
343
- placeholder="Enter or paste your DNA sequence here...\nExample: ATGAAACGCATTAGCACC...",
344
- lines=10,
345
- max_lines=20,
346
- show_copy_button=True,
347
- container=True
348
- )
349
-
350
- # Real-time sequence stats
351
- sequence_stats = gr.HTML(value="", elem_classes=["sequence-stats"])
352
-
353
- with gr.Row():
354
- submit_btn = gr.Button("🔬 Analyze Sequence", variant="primary", size="lg", scale=2)
355
- clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="lg", scale=1)
356
-
357
- # Example buttons
358
- gr.Markdown("### 📝 Quick Examples:")
359
- with gr.Row():
360
- example1_btn = gr.Button("Short Gene", variant="secondary", size="sm")
361
- example2_btn = gr.Button("Longer Sequence", variant="secondary", size="sm")
362
-
363
- with gr.Column(scale=2):
364
- output = gr.Textbox(
365
- label="🔬 Analysis Results",
366
- lines=25,
367
- max_lines=35,
368
- show_copy_button=True,
369
- container=True,
370
- placeholder="Results will appear here after analysis..."
371
- )
372
-
373
- # Footer
374
- gr.HTML('''
375
- <div style="text-align: center; margin-top: 2rem; padding: 1rem; border-top: 1px solid #dee2e6; color: #6c757d;">
376
- <small>🧬 Powered by boundary-aware deep learning | Built with PyTorch & Gradio</small>
377
- </div>
378
- ''')
379
-
380
- # Event handlers
381
- def update_stats(sequence):
382
- return get_sequence_stats(sequence)
383
-
384
- # Real-time sequence stats update
385
- sequence_input.change(
386
- fn=update_stats,
387
- inputs=sequence_input,
388
- outputs=sequence_stats
389
- )
390
-
391
- # Main prediction
392
- submit_btn.click(
393
- fn=predict_genes,
394
- inputs=sequence_input,
395
- outputs=output
396
- )
397
-
398
- # Clear functionality
399
- clear_btn.click(
400
- fn=lambda: ("", "", ""),
401
- outputs=[sequence_input, output, sequence_stats]
402
- )
403
-
404
- # Example buttons
405
- example1_btn.click(
406
- fn=lambda: examples[0][0],
407
- outputs=sequence_input
408
- )
409
-
410
- example2_btn.click(
411
- fn=lambda: examples[1][0],
412
- outputs=sequence_input
413
- )
414
-
415
- # Allow Enter key to submit
416
- sequence_input.submit(
417
- fn=predict_genes,
418
- inputs=sequence_input,
419
- outputs=output
420
- )
421
 
422
- # Launch configuration
423
  if __name__ == "__main__":
424
  print("🚀 Launching Gene Prediction App...")
 
 
 
 
425
  print(f"Model loaded: {model_loaded}")
426
  print(f"Open your browser to see the interface")
427
 
 
428
  demo.launch(
429
  server_name="0.0.0.0",
430
  server_port=7860,
 
 
431
  show_error=True,
432
- show_api=False,
433
- share=False, # Set to True if you want a public link
434
- inbrowser=True, # Automatically open browser
435
- prevent_thread_lock=False
436
  )
 
220
 
221
  return stats
222
 
223
+ def create_interface():
224
+ """Create the Gradio interface"""
225
+ # Initialize model on startup
226
+ print("Initializing model...")
227
+ model_status = initialize_model()
228
 
229
+ # Create custom CSS for better styling
230
+ custom_css = """
231
+ .gene-app {
232
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
233
+ }
234
 
235
+ .status-ready {
236
+ background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
237
+ border: 2px solid #28a745;
238
+ border-radius: 10px;
239
+ padding: 15px;
240
+ color: #155724;
241
+ font-weight: bold;
242
+ box-shadow: 0 2px 10px rgba(40, 167, 69, 0.2);
243
+ }
244
 
245
+ .status-error {
246
+ background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
247
+ border: 2px solid #dc3545;
248
+ border-radius: 10px;
249
+ padding: 15px;
250
+ color: #721c24;
251
+ font-weight: bold;
252
+ box-shadow: 0 2px 10px rgba(220, 53, 69, 0.2);
253
+ }
254
 
255
+ .main-title {
256
+ text-align: center;
257
+ background: linear-gradient(135deg, #2E8B57 0%, #20B2AA 100%);
258
+ -webkit-background-clip: text;
259
+ -webkit-text-fill-color: transparent;
260
+ background-clip: text;
261
+ font-size: 2.5rem;
262
+ font-weight: bold;
263
+ margin-bottom: 1rem;
264
+ }
265
 
266
+ .instructions {
267
+ background: #f8f9fa;
268
+ border-radius: 10px;
269
+ padding: 20px;
270
+ border-left: 4px solid #2E8B57;
271
+ margin: 1rem 0;
272
+ }
273
 
274
+ .sequence-stats {
275
+ font-size: 0.9rem;
276
+ color: #6c757d;
277
+ font-style: italic;
278
+ margin-top: 5px;
279
+ }
280
+ """
281
 
282
+ print("Creating Gradio interface...")
 
283
 
284
+ # Determine status message and styling
285
+ if model_loaded:
286
+ status_html = '''
287
+ <div class="status-ready">
288
+ <strong>✅ Model Status:</strong> Ready for gene prediction!<br>
289
+ <small>🔬 Boundary-aware deep learning model loaded successfully</small>
290
+ </div>
291
+ '''
292
+ else:
293
+ status_html = f'''
294
+ <div class="status-error">
295
+ <strong>❌ Model Status:</strong> Model initialization failed<br>
296
+ <small>📋 Details: {error_message}</small>
297
+ </div>
298
+ '''
299
 
300
+ # Example sequences
301
+ examples = [
302
+ # Short example with clear gene
303
+ ["ATGAAACGCATTAGCACCACCATTACCACCACCATCACCATTACCACAGGTAACGGTGCGGGCTGACGCGTACAGGAAACACAGAAAAAAGCCCGCACCTGACAGTGCGGGCTTTTTTTTTCGACCAAAGGTAACGAGGTAACAACCATGCGAGTGTTGAAGTTCGGCGGTACATCAGTGGCAAATGCAGAACGTTTTCTGCGTAA"],
304
+ # Longer example
305
+ ["ATGAAACGCATTAGCACCACCATTACCACCACCATCACCATTACCACAGGTAACGGTGCGGGCTGACGCGTACAGGAAACACAGAAAAAAGCCCGCACCTGACAGTGCGGGCTTTTTTTTTCGACCAAAGGTAACGAGGTAACAACCATGCGAGTGTTGAAGTTCGGCGGTACATCAGTGGCAAATGCAGAACGTTTTCTGCGTGTTGCCGATATTCTGGAAAGCAATGCCAGGCAGGGGCAGGTGGCCACCGTCCTCTCTGCCCCCGCCAAAATCACCAACCACCTGGTGGCGATGATTGAAAAAACCATTAGCGGCCAGGATGCTTTACCCAATATCAGCGATGCCGAACGTATTTTTGCCGAACTTTTGACGGGACTCGCCGCCGCCCAGCCGGGGTTCCCGCTGGCGCAATTGAAAACTTTCGTCGATCAGGAATTTGCCCAATAG"],
306
+ ]
307
 
308
+ # Create the interface with custom theme
309
+ with gr.Blocks(
310
+ title="🧬 Gene Prediction Tool",
311
+ theme=gr.themes.Soft(primary_hue="emerald", secondary_hue="teal"),
312
+ css=custom_css
313
+ ) as interface:
314
+
315
+ gr.HTML('<h1 class="main-title">🧬 Advanced Gene Prediction Tool</h1>')
316
+ gr.HTML('<p style="text-align: center; font-size: 1.1rem; color: #6c757d; margin-bottom: 2rem;">AI-powered boundary-aware gene detection system</p>')
317
+
318
+ gr.HTML(status_html)
319
+
320
+ with gr.Row():
321
+ gr.HTML('''
322
+ <div class="instructions">
323
+ <h3>🔬 How to Use:</h3>
324
+ <ol>
325
+ <li><strong>Enter DNA sequence:</strong> Paste your sequence using A, T, C, G, N characters</li>
326
+ <li><strong>Click Analyze:</strong> The AI model will predict gene regions</li>
327
+ <li><strong>Review results:</strong> View detected genes with positions, codons, and confidence</li>
328
+ </ol>
329
+
330
+ <h4>📏 Requirements:</h4>
331
+ <ul>
332
+ <li>Characters: Only A, T, C, G, N allowed</li>
333
+ <li>Length: 3 - 10,000 nucleotides</li>
334
+ <li>Format: Raw sequence (FASTA headers will be ignored)</li>
335
+ </ul>
336
+ </div>
337
+ ''')
338
+
339
+ with gr.Row():
340
+ with gr.Column(scale=1):
341
+ sequence_input = gr.Textbox(
342
+ label="🧬 DNA Sequence Input",
343
+ placeholder="Enter or paste your DNA sequence here...\nExample: ATGAAACGCATTAGCACC...",
344
+ lines=10,
345
+ max_lines=20,
346
+ show_copy_button=True,
347
+ container=True
348
+ )
349
+
350
+ # Real-time sequence stats
351
+ sequence_stats = gr.HTML(value="", elem_classes=["sequence-stats"])
352
+
353
+ with gr.Row():
354
+ submit_btn = gr.Button("🔬 Analyze Sequence", variant="primary", size="lg", scale=2)
355
+ clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="lg", scale=1)
356
+
357
+ # Example buttons
358
+ gr.Markdown("### 📝 Quick Examples:")
359
+ with gr.Row():
360
+ example1_btn = gr.Button("Short Gene", variant="secondary", size="sm")
361
+ example2_btn = gr.Button("Longer Sequence", variant="secondary", size="sm")
362
 
363
+ with gr.Column(scale=2):
364
+ output = gr.Textbox(
365
+ label="🔬 Analysis Results",
366
+ lines=25,
367
+ max_lines=35,
368
+ show_copy_button=True,
369
+ container=True,
370
+ placeholder="Results will appear here after analysis..."
371
+ )
372
+
373
+ # Footer
374
+ gr.HTML('''
375
+ <div style="text-align: center; margin-top: 2rem; padding: 1rem; border-top: 1px solid #dee2e6; color: #6c757d;">
376
+ <small>🧬 Powered by boundary-aware deep learning | Built with PyTorch & Gradio</small>
377
  </div>
378
  ''')
379
+
380
+ # Event handlers
381
+ def update_stats(sequence):
382
+ return get_sequence_stats(sequence)
383
+
384
+ # Real-time sequence stats update
385
+ sequence_input.change(
386
+ fn=update_stats,
387
+ inputs=sequence_input,
388
+ outputs=sequence_stats
389
+ )
390
+
391
+ # Main prediction
392
+ submit_btn.click(
393
+ fn=predict_genes,
394
+ inputs=sequence_input,
395
+ outputs=output
396
+ )
397
+
398
+ # Clear functionality
399
+ clear_btn.click(
400
+ fn=lambda: ("", "", ""),
401
+ outputs=[sequence_input, output, sequence_stats]
402
+ )
403
+
404
+ # Example buttons
405
+ example1_btn.click(
406
+ fn=lambda: examples[0][0],
407
+ outputs=sequence_input
408
+ )
409
+
410
+ example2_btn.click(
411
+ fn=lambda: examples[1][0],
412
+ outputs=sequence_input
413
+ )
414
+
415
+ # Allow Enter key to submit
416
+ sequence_input.submit(
417
+ fn=predict_genes,
418
+ inputs=sequence_input,
419
+ outputs=output
420
+ )
421
 
422
+ return interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
 
424
+ # Create and launch the interface
425
  if __name__ == "__main__":
426
  print("🚀 Launching Gene Prediction App...")
427
+
428
+ # Create the interface
429
+ demo = create_interface()
430
+
431
  print(f"Model loaded: {model_loaded}")
432
  print(f"Open your browser to see the interface")
433
 
434
+ # Launch with Hugging Face Spaces compatible settings
435
  demo.launch(
436
  server_name="0.0.0.0",
437
  server_port=7860,
438
+ share=False,
439
+ debug=False,
440
  show_error=True,
441
+ quiet=False
 
 
 
442
  )