Prak2005 commited on
Commit
604a199
·
verified ·
1 Parent(s): d531954

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -137
app.py CHANGED
@@ -1077,17 +1077,13 @@ def hello_world():
1077
 
1078
  def process_markdown(markdown_text, page_size, font_size, font_name,
1079
  margin_size, include_toc, color_theme, use_ai,
1080
- enhancement_instructions=None, processing_status=None):
1081
  """
1082
  Process markdown text and generate a PDF.
1083
 
1084
  Returns:
1085
  Path to generated PDF file
1086
  """
1087
- # Update status
1088
- if processing_status:
1089
- processing_status.update(value="Starting conversion process...")
1090
-
1091
  # Create a temporary file for the output
1092
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
1093
  output_path = temp_file.name
@@ -1096,10 +1092,6 @@ def process_markdown(markdown_text, page_size, font_size, font_name,
1096
  # Initialize the agent
1097
  agent = MarkdownToPDFAgent()
1098
 
1099
- # Update status
1100
- if processing_status:
1101
- processing_status.update(value="Setting up converter...")
1102
-
1103
  # Configure converter
1104
  agent.converter = MarkdownToPDFConverter(
1105
  output_path=output_path,
@@ -1115,21 +1107,12 @@ def process_markdown(markdown_text, page_size, font_size, font_name,
1115
  enhance = False
1116
 
1117
  if use_ai:
1118
- if processing_status:
1119
- processing_status.update(value="Setting up AI enhancement...")
1120
-
1121
  # Use the API key from Hugging Face secrets if available
1122
  api_key = GEMINI_API_KEY
1123
 
1124
  if api_key:
1125
- if processing_status:
1126
- processing_status.update(value="Connecting to Google Gemini API...")
1127
-
1128
  success = agent.setup_from_gemini(api_key)
1129
  enhance = success
1130
-
1131
- if not success and processing_status:
1132
- processing_status.update(value="Failed to connect to Google Gemini API. Converting without enhancement...")
1133
 
1134
  try:
1135
  # Create a temporary file for the markdown content
@@ -1137,11 +1120,6 @@ def process_markdown(markdown_text, page_size, font_size, font_name,
1137
  temp_md_path = temp_md_file.name
1138
  temp_md_file.write(markdown_text.encode('utf-8'))
1139
 
1140
- # Update status
1141
- if processing_status:
1142
- status_message = "Enhancing markdown content..." if enhance else "Converting markdown to PDF..."
1143
- processing_status.update(value=status_message)
1144
-
1145
  # Process the file
1146
  output_file = agent.process_file(
1147
  temp_md_path,
@@ -1155,28 +1133,18 @@ def process_markdown(markdown_text, page_size, font_size, font_name,
1155
  # Remove the temporary md file
1156
  os.unlink(temp_md_path)
1157
 
1158
- # Update status
1159
- if processing_status:
1160
- processing_status.update(value="Conversion complete! Your PDF is ready.")
1161
- time.sleep(1) # Give user time to see the completion message
1162
- processing_status.update(value="") # Clear the status
1163
-
1164
  if output_file:
1165
  return output_file
1166
  else:
1167
  return None
1168
  except Exception as e:
1169
  logger.error(f"Error processing markdown: {e}")
1170
- if processing_status:
1171
- processing_status.update(value=f"Error: {str(e)}")
1172
- time.sleep(2) # Show error briefly
1173
- processing_status.update(value="") # Clear the status
1174
  return None
1175
 
1176
  def upload_handler(files):
1177
  """Handle file uploads and extract markdown content"""
1178
  if not files:
1179
- return "No file uploaded"
1180
 
1181
  # Get the first file
1182
  file = files[0]
@@ -1219,28 +1187,10 @@ custom_css = """
1219
  color: #6c757d;
1220
  }
1221
 
1222
- .status-message {
1223
- padding: 0.75rem;
1224
- border-radius: 0.5rem;
1225
- margin-top: 1rem;
1226
- background-color: #e8f5e9;
1227
- color: #2E7D32;
1228
- text-align: center;
1229
- min-height: 1.5rem;
1230
- transition: opacity 0.3s;
1231
- }
1232
-
1233
  .template-selector {
1234
  margin-bottom: 1rem;
1235
  }
1236
 
1237
- .processing-message {
1238
- padding: 0.5rem;
1239
- font-style: italic;
1240
- color: #1976D2;
1241
- text-align: center;
1242
- }
1243
-
1244
  footer {
1245
  text-align: center;
1246
  margin-top: 2rem;
@@ -1257,7 +1207,7 @@ footer {
1257
  }
1258
  """
1259
 
1260
- # Define the Gradio interface
1261
  with gr.Blocks(title="MarkdownMuse", css=custom_css, theme=gr.themes.Soft()) as app:
1262
  # App header
1263
  gr.HTML(
@@ -1269,57 +1219,38 @@ with gr.Blocks(title="MarkdownMuse", css=custom_css, theme=gr.themes.Soft()) as
1269
  """
1270
  )
1271
 
1272
- # Create tabs for different input methods
1273
- with gr.Tabs() as tabs:
1274
- with gr.Tab("📝 Write Markdown"):
1275
- with gr.Row():
1276
- with gr.Column():
1277
- # Sample template selector
1278
- template_selector = gr.Dropdown(
1279
- choices=["Basic", "Report", "Documentation", "Article"],
1280
- value="Basic",
1281
- label="Choose a template",
1282
- elem_classes="template-selector"
1283
- )
1284
- sample_btn = gr.Button("📋 Load Template")
1285
-
1286
- # Markdown input
1287
- markdown_input = gr.TextArea(
1288
- placeholder="Enter your markdown content here...",
1289
- label="Markdown Content",
1290
- lines=15
1291
- )
1292
-
1293
- # Status message
1294
- processing_status = gr.Textbox(
1295
- label="Status",
1296
- value="",
1297
- elem_classes="status-message"
1298
- )
1299
-
1300
- with gr.Tab("📂 Upload Markdown"):
1301
- with gr.Row():
1302
- with gr.Column():
1303
- # File upload
1304
- uploaded_file = gr.File(
1305
- label="Upload Markdown File",
1306
- file_types=[".md", ".markdown", ".txt"],
1307
- file_count="single"
1308
- )
1309
-
1310
- upload_btn = gr.Button("📤 Load from File")
1311
-
1312
- # Markdown input (same as in Write tab)
1313
- upload_markdown_input = gr.TextArea(
1314
- placeholder="Content from uploaded file will appear here...",
1315
- label="Markdown Content",
1316
- lines=15
1317
- )
1318
-
1319
- # Settings panel
1320
- with gr.Accordion("⚙️ PDF Settings", open=False):
1321
- with gr.Row():
1322
- with gr.Column():
1323
  page_size = gr.Radio(
1324
  ["A4", "Letter"],
1325
  label="Page Size",
@@ -1333,8 +1264,7 @@ with gr.Blocks(title="MarkdownMuse", css=custom_css, theme=gr.themes.Soft()) as
1333
  step=1,
1334
  label="Base Font Size (pt)"
1335
  )
1336
-
1337
- with gr.Column():
1338
  font_name = gr.Dropdown(
1339
  ["Helvetica", "Times-Roman", "Courier"],
1340
  label="Font Family",
@@ -1349,42 +1279,36 @@ with gr.Blocks(title="MarkdownMuse", css=custom_css, theme=gr.themes.Soft()) as
1349
  label="Margins (inches)"
1350
  )
1351
 
1352
- with gr.Row():
1353
- with gr.Column():
1354
  include_toc = gr.Checkbox(
1355
  value=True,
1356
  label="Include Table of Contents"
1357
  )
1358
-
1359
- with gr.Column():
1360
  color_theme = gr.Dropdown(
1361
  ["Default", "Modern", "Professional", "Creative", "Elegant"],
1362
  value="Modern",
1363
  label="Color Theme"
1364
  )
1365
 
1366
- # AI Enhancement panel
1367
- with gr.Accordion("🧠 AI Enhancement", open=False):
1368
- with gr.Row():
1369
- with gr.Column():
1370
  use_ai = gr.Checkbox(
1371
  value=True,
1372
- label="Enable AI Enhancement with Google Gemini"
1373
  )
1374
 
1375
- # Show API key status (using the environment variable)
1376
  api_status = "API Key: Available from Hugging Face Secrets" if GEMINI_API_KEY else "API Key: Not configured"
1377
  gr.Markdown(f"**{api_status}**")
1378
 
1379
  enhancement_instructions = gr.TextArea(
1380
  placeholder="Optional: Provide specific instructions for how the AI should enhance your markdown...",
1381
- label="Enhancement Instructions (Optional)",
1382
  lines=3,
1383
  visible=True
1384
  )
1385
 
1386
  # Convert button and output
1387
- convert_btn = gr.Button("🔄 Convert to PDF", variant="primary")
1388
  output_pdf = gr.File(label="Generated PDF")
1389
 
1390
  # Set up event handlers
@@ -1397,29 +1321,14 @@ with gr.Blocks(title="MarkdownMuse", css=custom_css, theme=gr.themes.Soft()) as
1397
  upload_btn.click(
1398
  upload_handler,
1399
  inputs=[uploaded_file],
1400
- outputs=upload_markdown_input
1401
  )
1402
 
1403
- # Function to determine which markdown input to use
1404
- def get_active_markdown(tab_index, write_markdown, upload_markdown):
1405
- if tab_index == 0: # Write tab
1406
- return write_markdown
1407
- else: # Upload tab
1408
- return upload_markdown
1409
-
1410
  # Connect the conversion button to the processing function
1411
  convert_btn.click(
1412
- lambda tab_index, write_md, upload_md, page_size, font_size, font_name,
1413
- margin_size, include_toc, color_theme, use_ai, enhancement_instructions, processing_status:
1414
- process_markdown(
1415
- get_active_markdown(tab_index, write_md, upload_md),
1416
- page_size, font_size, font_name, margin_size, include_toc,
1417
- color_theme, use_ai, enhancement_instructions, processing_status
1418
- ),
1419
  inputs=[
1420
- tabs,
1421
  markdown_input,
1422
- upload_markdown_input,
1423
  page_size,
1424
  font_size,
1425
  font_name,
@@ -1427,8 +1336,7 @@ with gr.Blocks(title="MarkdownMuse", css=custom_css, theme=gr.themes.Soft()) as
1427
  include_toc,
1428
  color_theme,
1429
  use_ai,
1430
- enhancement_instructions,
1431
- processing_status
1432
  ],
1433
  outputs=output_pdf
1434
  )
 
1077
 
1078
  def process_markdown(markdown_text, page_size, font_size, font_name,
1079
  margin_size, include_toc, color_theme, use_ai,
1080
+ enhancement_instructions=None):
1081
  """
1082
  Process markdown text and generate a PDF.
1083
 
1084
  Returns:
1085
  Path to generated PDF file
1086
  """
 
 
 
 
1087
  # Create a temporary file for the output
1088
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
1089
  output_path = temp_file.name
 
1092
  # Initialize the agent
1093
  agent = MarkdownToPDFAgent()
1094
 
 
 
 
 
1095
  # Configure converter
1096
  agent.converter = MarkdownToPDFConverter(
1097
  output_path=output_path,
 
1107
  enhance = False
1108
 
1109
  if use_ai:
 
 
 
1110
  # Use the API key from Hugging Face secrets if available
1111
  api_key = GEMINI_API_KEY
1112
 
1113
  if api_key:
 
 
 
1114
  success = agent.setup_from_gemini(api_key)
1115
  enhance = success
 
 
 
1116
 
1117
  try:
1118
  # Create a temporary file for the markdown content
 
1120
  temp_md_path = temp_md_file.name
1121
  temp_md_file.write(markdown_text.encode('utf-8'))
1122
 
 
 
 
 
 
1123
  # Process the file
1124
  output_file = agent.process_file(
1125
  temp_md_path,
 
1133
  # Remove the temporary md file
1134
  os.unlink(temp_md_path)
1135
 
 
 
 
 
 
 
1136
  if output_file:
1137
  return output_file
1138
  else:
1139
  return None
1140
  except Exception as e:
1141
  logger.error(f"Error processing markdown: {e}")
 
 
 
 
1142
  return None
1143
 
1144
  def upload_handler(files):
1145
  """Handle file uploads and extract markdown content"""
1146
  if not files:
1147
+ return ""
1148
 
1149
  # Get the first file
1150
  file = files[0]
 
1187
  color: #6c757d;
1188
  }
1189
 
 
 
 
 
 
 
 
 
 
 
 
1190
  .template-selector {
1191
  margin-bottom: 1rem;
1192
  }
1193
 
 
 
 
 
 
 
 
1194
  footer {
1195
  text-align: center;
1196
  margin-top: 2rem;
 
1207
  }
1208
  """
1209
 
1210
+ # Define the Gradio interface - Using a simpler structure without tabs to fix the error
1211
  with gr.Blocks(title="MarkdownMuse", css=custom_css, theme=gr.themes.Soft()) as app:
1212
  # App header
1213
  gr.HTML(
 
1219
  """
1220
  )
1221
 
1222
+ # Two column layout - left for input, right for templates/settings
1223
+ with gr.Row():
1224
+ # Left column - Main input
1225
+ with gr.Column(scale=3):
1226
+ markdown_input = gr.TextArea(
1227
+ placeholder="Enter your markdown content here...",
1228
+ label="Markdown Content",
1229
+ lines=20,
1230
+ elem_id="markdown-input"
1231
+ )
1232
+
1233
+ # Upload file option
1234
+ gr.Markdown("### 📤 Or upload a file")
1235
+ uploaded_file = gr.File(
1236
+ label="Upload Markdown File",
1237
+ file_types=[".md", ".markdown", ".txt"],
1238
+ file_count="single"
1239
+ )
1240
+ upload_btn = gr.Button("Load from File")
1241
+
1242
+ # Right column - Templates and settings
1243
+ with gr.Column(scale=1):
1244
+ gr.Markdown("### 📋 Templates")
1245
+ template_selector = gr.Dropdown(
1246
+ choices=["Basic", "Report", "Documentation", "Article"],
1247
+ value="Basic",
1248
+ label="Choose a template",
1249
+ elem_classes="template-selector"
1250
+ )
1251
+ sample_btn = gr.Button("Load Template")
1252
+
1253
+ with gr.Accordion("⚙️ PDF Settings", open=True):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1254
  page_size = gr.Radio(
1255
  ["A4", "Letter"],
1256
  label="Page Size",
 
1264
  step=1,
1265
  label="Base Font Size (pt)"
1266
  )
1267
+
 
1268
  font_name = gr.Dropdown(
1269
  ["Helvetica", "Times-Roman", "Courier"],
1270
  label="Font Family",
 
1279
  label="Margins (inches)"
1280
  )
1281
 
 
 
1282
  include_toc = gr.Checkbox(
1283
  value=True,
1284
  label="Include Table of Contents"
1285
  )
1286
+
 
1287
  color_theme = gr.Dropdown(
1288
  ["Default", "Modern", "Professional", "Creative", "Elegant"],
1289
  value="Modern",
1290
  label="Color Theme"
1291
  )
1292
 
1293
+ with gr.Accordion("🧠 AI Enhancement", open=False):
 
 
 
1294
  use_ai = gr.Checkbox(
1295
  value=True,
1296
+ label="Enable AI Enhancement"
1297
  )
1298
 
1299
+ # Show API key status
1300
  api_status = "API Key: Available from Hugging Face Secrets" if GEMINI_API_KEY else "API Key: Not configured"
1301
  gr.Markdown(f"**{api_status}**")
1302
 
1303
  enhancement_instructions = gr.TextArea(
1304
  placeholder="Optional: Provide specific instructions for how the AI should enhance your markdown...",
1305
+ label="Enhancement Instructions",
1306
  lines=3,
1307
  visible=True
1308
  )
1309
 
1310
  # Convert button and output
1311
+ convert_btn = gr.Button("🔄 Convert to PDF", variant="primary", size="lg")
1312
  output_pdf = gr.File(label="Generated PDF")
1313
 
1314
  # Set up event handlers
 
1321
  upload_btn.click(
1322
  upload_handler,
1323
  inputs=[uploaded_file],
1324
+ outputs=markdown_input
1325
  )
1326
 
 
 
 
 
 
 
 
1327
  # Connect the conversion button to the processing function
1328
  convert_btn.click(
1329
+ process_markdown,
 
 
 
 
 
 
1330
  inputs=[
 
1331
  markdown_input,
 
1332
  page_size,
1333
  font_size,
1334
  font_name,
 
1336
  include_toc,
1337
  color_theme,
1338
  use_ai,
1339
+ enhancement_instructions
 
1340
  ],
1341
  outputs=output_pdf
1342
  )