nikajoon commited on
Commit
1762c84
1 Parent(s): eccce65

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +16 -5
api.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastapi import FastAPI, HTTPException, Request
2
- from fastapi.responses import HTMLResponse, JSONResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  import threading
5
  import gradio as gr
@@ -7,6 +7,8 @@ import torch
7
  import numpy as np
8
  from diffusers import DiffusionPipeline
9
  from transformers import pipeline
 
 
10
 
11
  app = FastAPI()
12
 
@@ -15,7 +17,7 @@ origins = [
15
  "http://localhost",
16
  "http://localhost:8000",
17
  "http://localhost:7860",
18
- "https://nikajoon-test1.hf.space",
19
  "http://tomko.ir", # دامنه سایت شما
20
  ]
21
 
@@ -67,9 +69,18 @@ async def generate_image(request: Request):
67
  raise HTTPException(status_code=400, detail="Prompt is required")
68
 
69
  image, _ = genie(prompt)
70
- # ذخیره‌سازی تصویر و بازگرداندن URL یا داده‌های تصویر به عنوان پاسخ
71
- image.save("output.png")
72
- return JSONResponse(content={"image_url": "output.png"})
 
 
 
 
 
 
 
 
 
73
 
74
  @app.get("/", response_class=HTMLResponse)
75
  async def read_root():
 
1
  from fastapi import FastAPI, HTTPException, Request
2
+ from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  import threading
5
  import gradio as gr
 
7
  import numpy as np
8
  from diffusers import DiffusionPipeline
9
  from transformers import pipeline
10
+ from PIL import Image
11
+ import os
12
 
13
  app = FastAPI()
14
 
 
17
  "http://localhost",
18
  "http://localhost:8000",
19
  "http://localhost:7860",
20
+ "https://nikajoon-test1.hf.space", # URL فضای شما در Hugging Face
21
  "http://tomko.ir", # دامنه سایت شما
22
  ]
23
 
 
69
  raise HTTPException(status_code=400, detail="Prompt is required")
70
 
71
  image, _ = genie(prompt)
72
+ image_path = "images/output.png"
73
+ os.makedirs(os.path.dirname(image_path), exist_ok=True)
74
+ image.save(image_path)
75
+
76
+ return JSONResponse(content={"image_url": f"/images/output.png"})
77
+
78
+ @app.get("/images/{image_name}", response_class=FileResponse)
79
+ async def get_image(image_name: str):
80
+ image_path = os.path.join("images", image_name)
81
+ if not os.path.exists(image_path):
82
+ raise HTTPException(status_code=404, detail="Image not found")
83
+ return FileResponse(image_path)
84
 
85
  @app.get("/", response_class=HTMLResponse)
86
  async def read_root():