ApsidalSolid4 commited on
Commit
5d66f23
·
verified ·
1 Parent(s): 50bed4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +388 -50
app.py CHANGED
@@ -18,6 +18,10 @@ from openpyxl.utils import get_column_letter
18
  from io import BytesIO
19
  import base64
20
  import hashlib
 
 
 
 
21
 
22
  # Configure logging
23
  logging.basicConfig(level=logging.INFO)
@@ -32,6 +36,17 @@ CONFIDENCE_THRESHOLD = 0.65
32
  BATCH_SIZE = 8 # Reduced batch size for CPU
33
  MAX_WORKERS = 4 # Number of worker threads for processing
34
 
 
 
 
 
 
 
 
 
 
 
 
35
  # Get password hash from environment variable (more secure)
36
  ADMIN_PASSWORD_HASH = os.environ.get('ADMIN_PASSWORD_HASH')
37
 
@@ -41,10 +56,138 @@ if not ADMIN_PASSWORD_HASH:
41
  # Excel file path for logs
42
  EXCEL_LOG_PATH = "/tmp/prediction_logs.xlsx"
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def is_admin_password(input_text: str) -> bool:
45
  """
46
  Check if the input text matches the admin password using secure hash comparison.
47
- This prevents the password from being visible in the source code.
48
  """
49
  # Hash the input text
50
  input_hash = hashlib.sha256(input_text.strip().encode()).hexdigest()
@@ -105,11 +248,6 @@ class TextWindowProcessor:
105
 
106
  class TextClassifier:
107
  def __init__(self):
108
- # Set thread configuration before any model loading or parallel work
109
- if not torch.cuda.is_available():
110
- torch.set_num_threads(MAX_WORKERS)
111
- torch.set_num_interop_threads(MAX_WORKERS)
112
-
113
  self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
114
  self.model_name = MODEL_NAME
115
  self.tokenizer = None
@@ -144,6 +282,7 @@ class TextClassifier:
144
 
145
  self.model.eval()
146
 
 
147
  def quick_scan(self, text: str) -> Dict:
148
  """Perform a quick scan using simple window analysis."""
149
  if not text.strip():
@@ -253,7 +392,7 @@ class TextClassifier:
253
  for window_idx, indices in enumerate(batch_indices):
254
  center_idx = len(indices) // 2
255
  center_weight = 0.7 # Higher weight for center sentence
256
- edge_weight = 0.3 / (len(indices) - 1) # Distribute remaining weight
257
 
258
  for pos, sent_idx in enumerate(indices):
259
  # Apply higher weight to center sentence
@@ -276,10 +415,10 @@ class TextClassifier:
276
 
277
  # Apply minimal smoothing at prediction boundaries
278
  if i > 0 and i < len(sentences) - 1:
279
- prev_human = sentence_scores[i-1]['human_prob'] / sentence_appearances[i-1]
280
- prev_ai = sentence_scores[i-1]['ai_prob'] / sentence_appearances[i-1]
281
- next_human = sentence_scores[i+1]['human_prob'] / sentence_appearances[i+1]
282
- next_ai = sentence_scores[i+1]['ai_prob'] / sentence_appearances[i+1]
283
 
284
  # Check if we're at a prediction boundary
285
  current_pred = 'human' if human_prob > ai_prob else 'ai'
@@ -354,6 +493,72 @@ class TextClassifier:
354
  'num_sentences': num_sentences
355
  }
356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
  def initialize_excel_log():
358
  """Initialize the Excel log file if it doesn't exist."""
359
  if not os.path.exists(EXCEL_LOG_PATH):
@@ -381,6 +586,7 @@ def initialize_excel_log():
381
  wb.save(EXCEL_LOG_PATH)
382
  logger.info(f"Initialized Excel log file at {EXCEL_LOG_PATH}")
383
 
 
384
  def log_prediction_data(input_text, word_count, prediction, confidence, execution_time, mode):
385
  """Log prediction data to an Excel file in the /tmp directory."""
386
  # Initialize the Excel file if it doesn't exist
@@ -423,6 +629,7 @@ def log_prediction_data(input_text, word_count, prediction, confidence, executio
423
  logger.error(f"Error logging prediction data to Excel: {str(e)}")
424
  return False
425
 
 
426
  def get_logs_as_base64():
427
  """Read the Excel logs file and return as base64 for downloading."""
428
  if not os.path.exists(EXCEL_LOG_PATH):
@@ -441,6 +648,7 @@ def get_logs_as_base64():
441
  logger.error(f"Error reading Excel logs: {str(e)}")
442
  return None
443
 
 
444
  def analyze_text(text: str, mode: str, classifier: TextClassifier) -> tuple:
445
  """Analyze text using specified mode and return formatted results."""
446
  # Check if the input text matches the admin password using secure comparison
@@ -563,47 +771,177 @@ def analyze_text(text: str, mode: str, classifier: TextClassifier) -> tuple:
563
  # Initialize the classifier globally
564
  classifier = TextClassifier()
565
 
566
- # Create Gradio interface
567
- demo = gr.Interface(
568
- fn=lambda text, mode: analyze_text(text, mode, classifier),
569
- inputs=[
570
- gr.Textbox(
571
- lines=8,
572
- placeholder="Enter text to analyze...",
573
- label="Input Text"
574
- ),
575
- gr.Radio(
576
- choices=["quick", "detailed"],
577
- value="quick",
578
- label="Analysis Mode",
579
- info="Quick mode for faster analysis, Detailed mode for sentence-level analysis"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580
  )
581
- ],
582
- outputs=[
583
- gr.HTML(label="Highlighted Analysis"),
584
- gr.Textbox(label="Sentence-by-Sentence Analysis", lines=10),
585
- gr.Textbox(label="Overall Result", lines=4)
586
- ],
587
- title="AI Text Detector",
588
- description="Analyze text to detect if it was written by a human or AI. Choose between quick scan and detailed sentence-level analysis. 200+ words suggested for accurate predictions.",
589
- api_name="predict",
590
- flagging_mode="never"
591
- )
592
-
593
- # Get the FastAPI app from Gradio
594
- app = demo.app
595
-
596
- # Add CORS middleware
597
- app.add_middleware(
598
- CORSMiddleware,
599
- allow_origins=["*"], # For development
600
- allow_credentials=True,
601
- allow_methods=["GET", "POST", "OPTIONS"],
602
- allow_headers=["*"],
603
- )
604
-
605
- # Ensure CORS is applied before launching
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
606
  if __name__ == "__main__":
 
 
 
607
  demo.queue()
608
  demo.launch(
609
  server_name="0.0.0.0",
 
18
  from io import BytesIO
19
  import base64
20
  import hashlib
21
+ import requests
22
+ import tempfile
23
+ from pathlib import Path
24
+ import mimetypes
25
 
26
  # Configure logging
27
  logging.basicConfig(level=logging.INFO)
 
36
  BATCH_SIZE = 8 # Reduced batch size for CPU
37
  MAX_WORKERS = 4 # Number of worker threads for processing
38
 
39
+ # IMPORTANT: Set PyTorch thread configuration at the module level
40
+ # before any parallel work starts
41
+ if not torch.cuda.is_available():
42
+ # Set thread configuration only once at the beginning
43
+ torch.set_num_threads(MAX_WORKERS)
44
+ try:
45
+ # Only set interop threads if it hasn't been set already
46
+ torch.set_num_interop_threads(MAX_WORKERS)
47
+ except RuntimeError as e:
48
+ logger.warning(f"Could not set interop threads: {str(e)}")
49
+
50
  # Get password hash from environment variable (more secure)
51
  ADMIN_PASSWORD_HASH = os.environ.get('ADMIN_PASSWORD_HASH')
52
 
 
56
  # Excel file path for logs
57
  EXCEL_LOG_PATH = "/tmp/prediction_logs.xlsx"
58
 
59
+ # OCR API settings
60
+ OCR_API_KEY = "9e11346f1288957" # This is a partial key - replace with the full one
61
+ OCR_API_ENDPOINT = "https://api.ocr.space/parse/image"
62
+ OCR_MAX_PDF_PAGES = 3
63
+ OCR_MAX_FILE_SIZE_MB = 1
64
+
65
+ # Configure logging for OCR module
66
+ ocr_logger = logging.getLogger("ocr_module")
67
+ ocr_logger.setLevel(logging.INFO)
68
+
69
+ class OCRProcessor:
70
+ """
71
+ Handles OCR processing of image and document files using OCR.space API
72
+ """
73
+ def __init__(self, api_key: str = OCR_API_KEY):
74
+ self.api_key = api_key
75
+ self.endpoint = OCR_API_ENDPOINT
76
+
77
+ def process_file(self, file_path: str) -> Dict:
78
+ """
79
+ Process a file using OCR.space API
80
+ """
81
+ start_time = time.time()
82
+ ocr_logger.info(f"Starting OCR processing for file: {os.path.basename(file_path)}")
83
+
84
+ # Validate file size
85
+ file_size_mb = os.path.getsize(file_path) / (1024 * 1024)
86
+ if file_size_mb > OCR_MAX_FILE_SIZE_MB:
87
+ ocr_logger.warning(f"File size ({file_size_mb:.2f} MB) exceeds limit of {OCR_MAX_FILE_SIZE_MB} MB")
88
+ return {
89
+ "success": False,
90
+ "error": f"File size ({file_size_mb:.2f} MB) exceeds limit of {OCR_MAX_FILE_SIZE_MB} MB",
91
+ "text": ""
92
+ }
93
+
94
+ # Determine file type and handle accordingly
95
+ file_type = self._get_file_type(file_path)
96
+ ocr_logger.info(f"Detected file type: {file_type}")
97
+
98
+ # Prepare the API request
99
+ with open(file_path, 'rb') as f:
100
+ file_data = f.read()
101
+
102
+ # Set up API parameters
103
+ payload = {
104
+ 'isOverlayRequired': 'false',
105
+ 'language': 'eng',
106
+ 'OCREngine': '2', # Use more accurate engine
107
+ 'scale': 'true',
108
+ 'detectOrientation': 'true',
109
+ }
110
+
111
+ # For PDF files, check page count limitations
112
+ if file_type == 'application/pdf':
113
+ ocr_logger.info("PDF document detected, enforcing page limit")
114
+ payload['filetype'] = 'PDF'
115
+
116
+ # Prepare file for OCR API
117
+ files = {
118
+ 'file': (os.path.basename(file_path), file_data, file_type)
119
+ }
120
+
121
+ headers = {
122
+ 'apikey': self.api_key,
123
+ }
124
+
125
+ # Make the OCR API request
126
+ try:
127
+ ocr_logger.info("Sending request to OCR.space API")
128
+ response = requests.post(
129
+ self.endpoint,
130
+ files=files,
131
+ data=payload,
132
+ headers=headers
133
+ )
134
+ response.raise_for_status()
135
+ result = response.json()
136
+
137
+ # Process the OCR results
138
+ if result.get('OCRExitCode') in [1, 2]: # Success or partial success
139
+ extracted_text = self._extract_text_from_result(result)
140
+ processing_time = time.time() - start_time
141
+ ocr_logger.info(f"OCR processing completed in {processing_time:.2f} seconds")
142
+
143
+ return {
144
+ "success": True,
145
+ "text": extracted_text,
146
+ "word_count": len(extracted_text.split()),
147
+ "processing_time_ms": int(processing_time * 1000)
148
+ }
149
+ else:
150
+ ocr_logger.error(f"OCR API error: {result.get('ErrorMessage', 'Unknown error')}")
151
+ return {
152
+ "success": False,
153
+ "error": result.get('ErrorMessage', 'OCR processing failed'),
154
+ "text": ""
155
+ }
156
+
157
+ except requests.exceptions.RequestException as e:
158
+ ocr_logger.error(f"OCR API request failed: {str(e)}")
159
+ return {
160
+ "success": False,
161
+ "error": f"OCR API request failed: {str(e)}",
162
+ "text": ""
163
+ }
164
+
165
+ def _extract_text_from_result(self, result: Dict) -> str:
166
+ """
167
+ Extract all text from the OCR API result
168
+ """
169
+ extracted_text = ""
170
+
171
+ if 'ParsedResults' in result and result['ParsedResults']:
172
+ for parsed_result in result['ParsedResults']:
173
+ if parsed_result.get('ParsedText'):
174
+ extracted_text += parsed_result['ParsedText']
175
+
176
+ return extracted_text
177
+
178
+ def _get_file_type(self, file_path: str) -> str:
179
+ """
180
+ Determine MIME type of a file
181
+ """
182
+ mime_type, _ = mimetypes.guess_type(file_path)
183
+ if mime_type is None:
184
+ # Default to binary if MIME type can't be determined
185
+ return 'application/octet-stream'
186
+ return mime_type
187
+
188
  def is_admin_password(input_text: str) -> bool:
189
  """
190
  Check if the input text matches the admin password using secure hash comparison.
 
191
  """
192
  # Hash the input text
193
  input_hash = hashlib.sha256(input_text.strip().encode()).hexdigest()
 
248
 
249
  class TextClassifier:
250
  def __init__(self):
 
 
 
 
 
251
  self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
252
  self.model_name = MODEL_NAME
253
  self.tokenizer = None
 
282
 
283
  self.model.eval()
284
 
285
+ # [Other TextClassifier methods remain the same as in paste.txt]
286
  def quick_scan(self, text: str) -> Dict:
287
  """Perform a quick scan using simple window analysis."""
288
  if not text.strip():
 
392
  for window_idx, indices in enumerate(batch_indices):
393
  center_idx = len(indices) // 2
394
  center_weight = 0.7 # Higher weight for center sentence
395
+ edge_weight = 0.3 / (len(indices) - 1) if len(indices) > 1 else 0 # Distribute remaining weight
396
 
397
  for pos, sent_idx in enumerate(indices):
398
  # Apply higher weight to center sentence
 
415
 
416
  # Apply minimal smoothing at prediction boundaries
417
  if i > 0 and i < len(sentences) - 1:
418
+ prev_human = sentence_scores[i-1]['human_prob'] / max(sentence_appearances[i-1], 1e-10)
419
+ prev_ai = sentence_scores[i-1]['ai_prob'] / max(sentence_appearances[i-1], 1e-10)
420
+ next_human = sentence_scores[i+1]['human_prob'] / max(sentence_appearances[i+1], 1e-10)
421
+ next_ai = sentence_scores[i+1]['ai_prob'] / max(sentence_appearances[i+1], 1e-10)
422
 
423
  # Check if we're at a prediction boundary
424
  current_pred = 'human' if human_prob > ai_prob else 'ai'
 
493
  'num_sentences': num_sentences
494
  }
495
 
496
+ # Function to handle file upload, OCR processing, and text analysis
497
+ def handle_file_upload_and_analyze(file_obj, mode: str, classifier) -> tuple:
498
+ """
499
+ Handle file upload, OCR processing, and text analysis
500
+ """
501
+ if file_obj is None:
502
+ return (
503
+ "No file uploaded",
504
+ "Please upload a file to analyze",
505
+ "No file uploaded for analysis"
506
+ )
507
+
508
+ # Create a temporary file with an appropriate extension based on content
509
+ content_start = file_obj[:20] # Look at the first few bytes
510
+
511
+ # Default to .bin extension
512
+ file_ext = ".bin"
513
+
514
+ # Try to detect PDF files
515
+ if content_start.startswith(b'%PDF'):
516
+ file_ext = ".pdf"
517
+ # For images, detect by common magic numbers
518
+ elif content_start.startswith(b'\xff\xd8'): # JPEG
519
+ file_ext = ".jpg"
520
+ elif content_start.startswith(b'\x89PNG'): # PNG
521
+ file_ext = ".png"
522
+ elif content_start.startswith(b'GIF'): # GIF
523
+ file_ext = ".gif"
524
+
525
+ # Create a temporary file with the detected extension
526
+ with tempfile.NamedTemporaryFile(delete=False, suffix=file_ext) as temp_file:
527
+ temp_file_path = temp_file.name
528
+ # Write uploaded file data to the temporary file
529
+ temp_file.write(file_obj)
530
+
531
+ try:
532
+ # Process the file with OCR
533
+ ocr_processor = OCRProcessor()
534
+ ocr_result = ocr_processor.process_file(temp_file_path)
535
+
536
+ if not ocr_result["success"]:
537
+ return (
538
+ "OCR Processing Error",
539
+ ocr_result["error"],
540
+ "Failed to extract text from the uploaded file"
541
+ )
542
+
543
+ # Get the extracted text
544
+ extracted_text = ocr_result["text"]
545
+
546
+ # If no text was extracted
547
+ if not extracted_text.strip():
548
+ return (
549
+ "No text extracted",
550
+ "The OCR process did not extract any text from the uploaded file.",
551
+ "No text was found in the uploaded file"
552
+ )
553
+
554
+ # Call the original text analysis function with the extracted text
555
+ return analyze_text(extracted_text, mode, classifier)
556
+
557
+ finally:
558
+ # Clean up the temporary file
559
+ if os.path.exists(temp_file_path):
560
+ os.remove(temp_file_path)
561
+
562
  def initialize_excel_log():
563
  """Initialize the Excel log file if it doesn't exist."""
564
  if not os.path.exists(EXCEL_LOG_PATH):
 
586
  wb.save(EXCEL_LOG_PATH)
587
  logger.info(f"Initialized Excel log file at {EXCEL_LOG_PATH}")
588
 
589
+
590
  def log_prediction_data(input_text, word_count, prediction, confidence, execution_time, mode):
591
  """Log prediction data to an Excel file in the /tmp directory."""
592
  # Initialize the Excel file if it doesn't exist
 
629
  logger.error(f"Error logging prediction data to Excel: {str(e)}")
630
  return False
631
 
632
+
633
  def get_logs_as_base64():
634
  """Read the Excel logs file and return as base64 for downloading."""
635
  if not os.path.exists(EXCEL_LOG_PATH):
 
648
  logger.error(f"Error reading Excel logs: {str(e)}")
649
  return None
650
 
651
+
652
  def analyze_text(text: str, mode: str, classifier: TextClassifier) -> tuple:
653
  """Analyze text using specified mode and return formatted results."""
654
  # Check if the input text matches the admin password using secure comparison
 
771
  # Initialize the classifier globally
772
  classifier = TextClassifier()
773
 
774
+ # Create Gradio interface with a small file upload button next to the radio buttons
775
+ # Modified approach - simplify by using custom HTML/CSS to achieve the exact layout
776
+ def setup_interface():
777
+ # Create analyzer functions that capture the classifier
778
+ def analyze_text_wrapper(text, mode):
779
+ return analyze_text(text, mode, classifier)
780
+
781
+ def handle_file_upload_wrapper(file_obj, mode):
782
+ if file_obj is None:
783
+ return analyze_text_wrapper("", mode)
784
+ return handle_file_upload_and_analyze(file_obj, mode, classifier)
785
+
786
+ with gr.Blocks(title="AI Text Detector", css="#analyze-btn { background-color: #FF8C00 !important; }") as demo:
787
+ gr.Markdown("# AI Text Detector")
788
+
789
+ with gr.Row():
790
+ # Left column - Input
791
+ with gr.Column(scale=1):
792
+ text_input = gr.Textbox(
793
+ lines=8,
794
+ placeholder="Enter text to analyze...",
795
+ label="Input Text"
796
+ )
797
+
798
+ gr.Markdown("Analysis Mode")
799
+ gr.Markdown("Quick mode for faster analysis. Detailed mode for sentence-level analysis.",
800
+ elem_classes=["description-text"])
801
+
802
+ with gr.Row():
803
+ with gr.Column(scale=12):
804
+ mode_selection = gr.Radio(
805
+ choices=["quick", "detailed"],
806
+ value="quick",
807
+ label=""
808
+ )
809
+
810
+ # Hidden file upload that will be repositioned with CSS
811
+ with gr.Column(scale=1, elem_id="file-upload-column"):
812
+ file_upload = gr.File(
813
+ file_types=["image", "pdf", "doc", "docx"],
814
+ type="binary",
815
+ label="",
816
+ elem_id="hidden-file-upload"
817
+ )
818
+
819
+ analyze_button = gr.Button("Analyze Text", elem_id="analyze-btn")
820
+
821
+ # Right column - Results
822
+ with gr.Column(scale=1):
823
+ output_html = gr.HTML(label="Highlighted Analysis")
824
+ output_sentences = gr.Textbox(label="Sentence-by-Sentence Analysis", lines=10)
825
+ output_result = gr.Textbox(label="Overall Result", lines=4)
826
+
827
+ # Connect the components
828
+ analyze_button.click(
829
+ analyze_text_wrapper,
830
+ inputs=[text_input, mode_selection],
831
+ outputs=[output_html, output_sentences, output_result]
832
+ )
833
+
834
+ file_upload.change(
835
+ handle_file_upload_wrapper,
836
+ inputs=[file_upload, mode_selection],
837
+ outputs=[output_html, output_sentences, output_result]
838
  )
839
+
840
+ # Applying extensive CSS to completely hide the file upload drop area
841
+ # and replace it with a small paperclip
842
+ gr.HTML("""
843
+ <style>
844
+ /* Make analyze button orange */
845
+ #analyze-btn {
846
+ background-color: #FF8C00 !important;
847
+ border-color: #FF8C00 !important;
848
+ }
849
+
850
+ /* File upload container styling */
851
+ #file-upload-column {
852
+ position: relative;
853
+ width: 32px !important;
854
+ padding-left: 0 !important;
855
+ padding-right: 0 !important;
856
+ margin-top: 8px;
857
+ }
858
+
859
+ /* Hide all default file upload elements */
860
+ #hidden-file-upload > * {
861
+ display: none !important;
862
+ }
863
+
864
+ #hidden-file-upload {
865
+ position: relative;
866
+ width: 32px !important;
867
+ height: 32px !important;
868
+ padding: 0 !important;
869
+ margin: 0 !important;
870
+ }
871
+
872
+ /* Create our custom paperclip button */
873
+ #hidden-file-upload::before {
874
+ content: "📎";
875
+ position: absolute;
876
+ top: 2px;
877
+ left: 8px;
878
+ font-size: 20px;
879
+ cursor: pointer;
880
+ z-index: 999;
881
+ opacity: 0.7;
882
+ }
883
+
884
+ #hidden-file-upload::before:hover {
885
+ opacity: 1;
886
+ }
887
+
888
+ /* Make only the actual input button clickable but invisible */
889
+ #hidden-file-upload .upload-button {
890
+ position: absolute !important;
891
+ width: 32px !important;
892
+ height: 32px !important;
893
+ opacity: 0 !important;
894
+ cursor: pointer !important;
895
+ z-index: 1000 !important;
896
+ margin: 0 !important;
897
+ padding: 0 !important;
898
+ }
899
+
900
+ /* Hide everything else in the file upload container */
901
+ #hidden-file-upload .hide,
902
+ #hidden-file-upload p,
903
+ #hidden-file-upload > .wrap > div:not(.upload-button),
904
+ #hidden-file-upload > label,
905
+ #hidden-file-upload > .wrap > p,
906
+ #hidden-file-upload .file-preview,
907
+ #hidden-file-upload .absolute {
908
+ display: none !important;
909
+ }
910
+
911
+ /* Make description text smaller */
912
+ .description-text {
913
+ font-size: 0.85em !important;
914
+ color: #666 !important;
915
+ margin-top: -10px !important;
916
+ margin-bottom: 5px !important;
917
+ }
918
+ </style>
919
+ """)
920
+
921
+ return demo
922
+ # Setup the app with CORS middleware
923
+ def setup_app():
924
+ demo = setup_interface()
925
+
926
+ # Get the FastAPI app from Gradio
927
+ app = demo.app
928
+
929
+ # Add CORS middleware
930
+ app.add_middleware(
931
+ CORSMiddleware,
932
+ allow_origins=["*"], # For development
933
+ allow_credentials=True,
934
+ allow_methods=["GET", "POST", "OPTIONS"],
935
+ allow_headers=["*"],
936
+ )
937
+
938
+ return demo
939
+
940
+ # Initialize the application
941
  if __name__ == "__main__":
942
+ demo = setup_app()
943
+
944
+ # Start the server
945
  demo.queue()
946
  demo.launch(
947
  server_name="0.0.0.0",