VirtualOasis commited on
Commit
6ffa77a
Β·
verified Β·
1 Parent(s): 17b2eb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -83
app.py CHANGED
@@ -21,7 +21,7 @@ def draw_lattice_grid(draw, width, height, spacing=100, color=(235, 235, 235)):
21
  # Draw vertical lines
22
  for x in range(0, width, spacing):
23
  draw.line([(x, 0), (x, height)], fill=color, width=2)
24
- # Draw horizontal lines
25
  for y in range(0, height, spacing):
26
  draw.line([(0, y), (width, y)], fill=color, width=2)
27
 
@@ -240,89 +240,111 @@ Every midnight, as the city slept, the clock would chime, not with bells, but wi
240
 
241
  One day, a young girl with eyes as curious as a cat's wandered into his shop. She wasn't interested in the shiny new watches but was drawn to the grandfather clock. "What's its story?" she asked, her voice soft. Alistair smiled, for he knew he had found the next guardian of the stories. The legacy of the whispering clock would live on."""
242
 
243
- demo = gr.Interface(
244
- fn=text_to_images_mcp,
245
- inputs=[
246
- gr.Textbox(
247
- value=sample_text,
248
- lines=10,
249
- max_lines=20,
250
- label="Text Content",
251
- placeholder="Enter your long-form text here (max 10,000 characters)..."
252
- ),
253
- gr.Dropdown(
254
- choices=['plain', 'lines', 'dots', 'grid'],
255
- value='lines',
256
- label="Background Style",
257
- info="Choose the background style for your images"
258
- ),
259
- gr.Textbox(
260
- value="",
261
- label="Custom Font Path (Optional)",
262
- placeholder="/path/to/your/font.ttf",
263
- info="Leave empty to use system default font"
264
- )
265
- ],
266
- outputs=[
267
- gr.Gallery(
268
- label="Generated Images (Max 10 pages)",
269
- show_label=True,
270
- elem_id="gallery",
271
- columns=2,
272
- rows=2,
273
- object_fit="contain",
274
- height="auto",
275
- show_download_button=True
276
- )
277
- ],
278
- title="πŸ“– Text to Images Converter",
279
- description="Transform long-form text into a series of attractive, readable images. Simply paste your text, choose a background style, and preview the generated images. You can download individual images by clicking on them. Limited to 10,000 characters and 10 pages for optimal performance.",
280
- examples=[
281
- [sample_text, 'lines', ''],
282
- [sample_text, 'dots', ''],
283
- [sample_text, 'grid', ''],
284
- [sample_text, 'plain', '']
285
- ],
286
- cache_examples=False
287
- )
 
 
 
 
 
 
 
288
 
289
  if __name__ == "__main__":
290
- import os
291
- import uvicorn
292
- from contextlib import asynccontextmanager
293
 
294
- # Try multiple approaches to fix the MCP server issues
295
- try:
296
- # Approach 1: Use environment variable instead of parameter
297
- os.environ["GRADIO_MCP_SERVER"] = "True"
298
-
299
- # Approach 2: Launch with specific server settings for better stability
300
- demo.launch(
301
- server_name="0.0.0.0",
302
- server_port=7860,
303
- share=False,
304
- debug=False,
305
- show_error=True,
306
- show_api=True,
307
- quiet=False,
308
- # Remove mcp_server=True temporarily to avoid ASGI issues
309
- # mcp_server=True
310
- )
311
- except Exception as e:
312
- print(f"Primary launch failed: {e}")
313
- print("Trying alternative launch method...")
314
-
315
- # Approach 3: Fallback launch without MCP initially
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  try:
317
- demo.launch(
318
- server_name="0.0.0.0",
319
- server_port=7860,
320
- share=False,
321
- debug=False
322
- )
323
- except Exception as e2:
324
- print(f"Fallback launch also failed: {e2}")
325
- print("Trying minimal launch...")
326
-
327
- # Approach 4: Minimal launch
328
- demo.launch()
 
 
 
21
  # Draw vertical lines
22
  for x in range(0, width, spacing):
23
  draw.line([(x, 0), (x, height)], fill=color, width=2)
24
+ # Draw horizontal lines
25
  for y in range(0, height, spacing):
26
  draw.line([(0, y), (width, y)], fill=color, width=2)
27
 
 
240
 
241
  One day, a young girl with eyes as curious as a cat's wandered into his shop. She wasn't interested in the shiny new watches but was drawn to the grandfather clock. "What's its story?" she asked, her voice soft. Alistair smiled, for he knew he had found the next guardian of the stories. The legacy of the whispering clock would live on."""
242
 
243
+ def create_app():
244
+ """Create and configure the Gradio app with better error handling."""
245
+
246
+ # Create the demo with enhanced error handling
247
+ demo = gr.Interface(
248
+ fn=text_to_images_mcp,
249
+ inputs=[
250
+ gr.Textbox(
251
+ value=sample_text,
252
+ lines=10,
253
+ max_lines=20,
254
+ label="Text Content",
255
+ placeholder="Enter your long-form text here (max 10,000 characters)..."
256
+ ),
257
+ gr.Dropdown(
258
+ choices=['plain', 'lines', 'dots', 'grid'],
259
+ value='lines',
260
+ label="Background Style",
261
+ info="Choose the background style for your images"
262
+ ),
263
+ gr.Textbox(
264
+ value="",
265
+ label="Custom Font Path (Optional)",
266
+ placeholder="/path/to/your/font.ttf",
267
+ info="Leave empty to use system default font"
268
+ )
269
+ ],
270
+ outputs=[
271
+ gr.Gallery(
272
+ label="Generated Images (Max 10 pages)",
273
+ show_label=True,
274
+ elem_id="gallery",
275
+ columns=2,
276
+ rows=2,
277
+ object_fit="contain",
278
+ height="auto",
279
+ show_download_button=True
280
+ )
281
+ ],
282
+ title="πŸ“– Text to Images Converter (MCP Compatible)",
283
+ description="Transform long-form text into a series of attractive, readable images. Simply paste your text, choose a background style, and preview the generated images. You can download individual images by clicking on them. Limited to 10,000 characters and 10 pages for optimal performance. This app functions as an MCP server for LLM integration.",
284
+ examples=[
285
+ [sample_text, 'lines', ''],
286
+ [sample_text, 'dots', ''],
287
+ [sample_text, 'grid', ''],
288
+ [sample_text, 'plain', '']
289
+ ],
290
+ cache_examples=False,
291
+ show_api=True
292
+ )
293
+
294
+ return demo
295
 
296
  if __name__ == "__main__":
297
+ # Set up environment for MCP server
298
+ os.environ["GRADIO_MCP_SERVER"] = "True"
 
299
 
300
+ # Create the app
301
+ demo = create_app()
302
+
303
+ # Launch with multiple fallback strategies
304
+ launch_configs = [
305
+ # Strategy 1: Full MCP server with explicit settings
306
+ {
307
+ "mcp_server": True,
308
+ "server_name": "0.0.0.0",
309
+ "server_port": 7860,
310
+ "share": False,
311
+ "debug": False,
312
+ "show_error": False,
313
+ "quiet": True
314
+ },
315
+ # Strategy 2: Environment variable only
316
+ {
317
+ "server_name": "0.0.0.0",
318
+ "server_port": 7860,
319
+ "share": False,
320
+ "debug": False,
321
+ "show_error": False,
322
+ "quiet": True
323
+ },
324
+ # Strategy 3: Minimal configuration
325
+ {
326
+ "server_name": "0.0.0.0",
327
+ "server_port": 7860,
328
+ "share": False
329
+ },
330
+ # Strategy 4: Default configuration
331
+ {}
332
+ ]
333
+
334
+ launched = False
335
+ for i, config in enumerate(launch_configs):
336
  try:
337
+ print(f"Attempting launch strategy {i+1}...")
338
+ demo.launch(**config)
339
+ launched = True
340
+ print(f"Successfully launched with strategy {i+1}")
341
+ break
342
+ except Exception as e:
343
+ print(f"Launch strategy {i+1} failed: {e}")
344
+ if i < len(launch_configs) - 1:
345
+ print("Trying next strategy...")
346
+ continue
347
+
348
+ if not launched:
349
+ print("All launch strategies failed. Please check your environment and dependencies.")
350
+ print("You may need to install gradio[mcp] or update your gradio version.")