randydev commited on
Commit
ce4ee40
1 Parent(s): cf13354

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +14 -7
main.py CHANGED
@@ -46,7 +46,7 @@ from telegraph import Telegraph, upload_file
46
  from pathlib import Path
47
  from serpapi import GoogleSearch
48
 
49
- from fastapi import FastAPI, UploadFile, File
50
  from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
51
  from fastapi import Depends, FastAPI, HTTPException, status
52
  from fastapi.openapi.utils import get_openapi
@@ -377,17 +377,24 @@ def ryuzaki_ai(
377
  return {"status": "false", "message": "Internal server error"}
378
 
379
  @app.get("/ryuzaki/unsplash")
380
- async def get_image_unsplash(query: str, size: str="500x500"):
381
  url = SOURCE_UNSPLASH_URL
382
  image_url = f"{url}/?{query}/{size}"
383
-
384
  try:
385
  response = requests.get(image_url)
386
  response.raise_for_status()
387
- except requests.exceptions.RequestException:
388
- raise HTTPException(status_code=500, detail="Internal server error")
389
-
390
- return StreamingResponse(BytesIO(response.content), media_type="image/jpeg")
 
 
 
 
 
 
 
 
391
 
392
  @app.get("/ryuzaki/calligraphy")
393
  async def get_image_calligraphy(query: str):
 
46
  from pathlib import Path
47
  from serpapi import GoogleSearch
48
 
49
+ from fastapi import FastAPI, UploadFile, File, Response
50
  from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
51
  from fastapi import Depends, FastAPI, HTTPException, status
52
  from fastapi.openapi.utils import get_openapi
 
377
  return {"status": "false", "message": "Internal server error"}
378
 
379
  @app.get("/ryuzaki/unsplash")
380
+ async def get_image_unsplash(query: str, size: str = "500x500"):
381
  url = SOURCE_UNSPLASH_URL
382
  image_url = f"{url}/?{query}/{size}"
 
383
  try:
384
  response = requests.get(image_url)
385
  response.raise_for_status()
386
+ except requests.exceptions.RequestException as e:
387
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
388
+ try:
389
+ encoded_string = base64.b64encode(response.content).decode("utf-8")
390
+ except Exception as e:
391
+ raise HTTPException(status_code=500, detail=f"Error encoding image: {str(e)}")
392
+ headers = {"Content-Type": "image/jpeg"}
393
+ return Response(
394
+ content=encoded_string,
395
+ media_type="image/jpeg",
396
+ headers=headers
397
+ )
398
 
399
  @app.get("/ryuzaki/calligraphy")
400
  async def get_image_calligraphy(query: str):