Norcoo commited on
Commit
cf4adf9
·
verified ·
1 Parent(s): ea08e0a

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +81 -91
  2. requirements.txt +0 -2
app.py CHANGED
@@ -10,7 +10,6 @@ import uuid
10
  import threading
11
  import time
12
  import random
13
- from fastapi import FastAPI, Response
14
  from fastapi.responses import FileResponse, JSONResponse
15
 
16
  # 环境变量
@@ -396,90 +395,6 @@ init_success = init_db()
396
  keep_alive_thread = threading.Thread(target=keep_alive, daemon=True)
397
  keep_alive_thread.start()
398
 
399
- # 创建FastAPI app
400
- fastapi_app = FastAPI()
401
-
402
- # 添加图片访问API端点
403
- @fastapi_app.get("/api/img/{image_hash}")
404
- async def get_image(image_hash: str):
405
- """通过hash获取图片"""
406
- try:
407
- conn = get_db_connection()
408
- cursor = conn.cursor()
409
- cursor.execute("SELECT * FROM images WHERE hash = ?", (image_hash,))
410
- row = cursor.fetchone()
411
- conn.close()
412
-
413
- if not row:
414
- return JSONResponse(
415
- status_code=404,
416
- content={"error": "Image not found"}
417
- )
418
-
419
- file_path = Path(row['file_path'])
420
-
421
- if not file_path.exists():
422
- return JSONResponse(
423
- status_code=404,
424
- content={"error": "Image file not found"}
425
- )
426
-
427
- # 返回图片文件
428
- return FileResponse(
429
- file_path,
430
- media_type=row['mime_type'],
431
- headers={
432
- "Cache-Control": "public, max-age=31536000",
433
- "Content-Disposition": f'inline; filename="{row["original_filename"]}"'
434
- }
435
- )
436
-
437
- except Exception as e:
438
- return JSONResponse(
439
- status_code=500,
440
- content={"error": str(e)}
441
- )
442
-
443
- # 添加图片信息API端点(可选,用于获取元数据)
444
- @fastapi_app.get("/api/img/{image_hash}/info")
445
- async def get_image_info(image_hash: str, password: str = None):
446
- """获取图片信息(需要密码)"""
447
- if not password or not check_password(password):
448
- return JSONResponse(
449
- status_code=401,
450
- content={"error": "Unauthorized"}
451
- )
452
-
453
- try:
454
- conn = get_db_connection()
455
- cursor = conn.cursor()
456
- cursor.execute("SELECT * FROM images WHERE hash = ?", (image_hash,))
457
- row = cursor.fetchone()
458
- conn.close()
459
-
460
- if not row:
461
- return JSONResponse(
462
- status_code=404,
463
- content={"error": "Image not found"}
464
- )
465
-
466
- return JSONResponse(content={
467
- "hash": row['hash'],
468
- "filename": row['filename'],
469
- "original_filename": row['original_filename'],
470
- "file_size": row['file_size'],
471
- "mime_type": row['mime_type'],
472
- "upload_time": row['upload_time'],
473
- "description": row['description'],
474
- "url": generate_full_url(row['hash'])
475
- })
476
-
477
- except Exception as e:
478
- return JSONResponse(
479
- status_code=500,
480
- content={"error": str(e)}
481
- )
482
-
483
  # 自定义CSS
484
  custom_css = """
485
  .compact-gallery .gallery {
@@ -625,10 +540,85 @@ with gr.Blocks(title="My图床", theme=gr.themes.Soft(), css=custom_css) as grad
625
  outputs=[export_output, export_file_output]
626
  )
627
 
628
- if __name__ == "__main__":
629
- # 将Gradio app挂载到FastAPI
630
- app = gr.mount_gradio_app(fastapi_app, gradio_app, path="/")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631
 
632
- # 启动服务
633
- import uvicorn
634
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  import threading
11
  import time
12
  import random
 
13
  from fastapi.responses import FileResponse, JSONResponse
14
 
15
  # 环境变量
 
395
  keep_alive_thread = threading.Thread(target=keep_alive, daemon=True)
396
  keep_alive_thread.start()
397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
  # 自定义CSS
399
  custom_css = """
400
  .compact-gallery .gallery {
 
540
  outputs=[export_output, export_file_output]
541
  )
542
 
543
+ # 添加FastAPI路由到Gradio的底层FastAPI应用
544
+ @gradio_app.app.get("/api/img/{image_hash}")
545
+ async def get_image(image_hash: str):
546
+ """通过hash获取图片"""
547
+ try:
548
+ conn = get_db_connection()
549
+ cursor = conn.cursor()
550
+ cursor.execute("SELECT * FROM images WHERE hash = ?", (image_hash,))
551
+ row = cursor.fetchone()
552
+ conn.close()
553
+
554
+ if not row:
555
+ return JSONResponse(
556
+ status_code=404,
557
+ content={"error": "Image not found"}
558
+ )
559
+
560
+ file_path = Path(row['file_path'])
561
+
562
+ if not file_path.exists():
563
+ return JSONResponse(
564
+ status_code=404,
565
+ content={"error": "Image file not found"}
566
+ )
567
+
568
+ # 返回图片文件
569
+ return FileResponse(
570
+ file_path,
571
+ media_type=row['mime_type'],
572
+ headers={
573
+ "Cache-Control": "public, max-age=31536000",
574
+ "Content-Disposition": f'inline; filename="{row["original_filename"]}"'
575
+ }
576
+ )
577
+
578
+ except Exception as e:
579
+ return JSONResponse(
580
+ status_code=500,
581
+ content={"error": str(e)}
582
+ )
583
+
584
+ @gradio_app.app.get("/api/img/{image_hash}/info")
585
+ async def get_image_info(image_hash: str, password: str = None):
586
+ """获取图片信息(需要密码)"""
587
+ if not password or not check_password(password):
588
+ return JSONResponse(
589
+ status_code=401,
590
+ content={"error": "Unauthorized"}
591
+ )
592
 
593
+ try:
594
+ conn = get_db_connection()
595
+ cursor = conn.cursor()
596
+ cursor.execute("SELECT * FROM images WHERE hash = ?", (image_hash,))
597
+ row = cursor.fetchone()
598
+ conn.close()
599
+
600
+ if not row:
601
+ return JSONResponse(
602
+ status_code=404,
603
+ content={"error": "Image not found"}
604
+ )
605
+
606
+ return JSONResponse(content={
607
+ "hash": row['hash'],
608
+ "filename": row['filename'],
609
+ "original_filename": row['original_filename'],
610
+ "file_size": row['file_size'],
611
+ "mime_type": row['mime_type'],
612
+ "upload_time": row['upload_time'],
613
+ "description": row['description'],
614
+ "url": generate_full_url(row['hash'])
615
+ })
616
+
617
+ except Exception as e:
618
+ return JSONResponse(
619
+ status_code=500,
620
+ content={"error": str(e)}
621
+ )
622
+
623
+ if __name__ == "__main__":
624
+ gradio_app.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt CHANGED
@@ -1,5 +1,3 @@
1
  gradio==4.44.0
2
  Pillow==10.4.0
3
- uvicorn[standard]>=0.24.0
4
- fastapi>=0.104.0
5
 
 
1
  gradio==4.44.0
2
  Pillow==10.4.0
 
 
3