Update main.py
Browse files
main.py
CHANGED
|
@@ -89,33 +89,22 @@ LANGUAGE_MAP = {
|
|
| 89 |
# Add more language mappings as needed
|
| 90 |
}
|
| 91 |
|
| 92 |
-
|
| 93 |
-
async def generate_story(image_file: UploadFile = File(...), language: str = Query("en")):
|
| 94 |
-
# Validate the language code
|
| 95 |
-
if language.lower() not in LANGUAGE_MAP:
|
| 96 |
-
return {"error": f"Translation to '{language}' is not supported."}
|
| 97 |
-
|
| 98 |
-
# Get the corresponding target language code
|
| 99 |
-
target_language = LANGUAGE_MAP[language.lower()]
|
| 100 |
|
|
|
|
|
|
|
| 101 |
image_data = await image_file.read()
|
| 102 |
system_prompt = f"write an attractive story in 300 words about {random.choice(['an adventurous journey', 'a mysterious encounter', 'a heroic quest'])}"
|
| 103 |
caption = generate_image_caption(image_data)
|
| 104 |
if caption.startswith("Error"):
|
| 105 |
raise HTTPException(status_code=500, detail=caption)
|
| 106 |
ai_response = next(run(caption, [], system_prompt))
|
| 107 |
-
|
| 108 |
-
# Translate the story if
|
| 109 |
-
if
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
translated_story = translator.translate(ai_response)
|
| 114 |
-
return {"story": translated_story}
|
| 115 |
-
except InvalidSourceOrTargetLanguage:
|
| 116 |
-
# Handle invalid language
|
| 117 |
-
return {"error": f"Translation to '{language}' is not supported."}
|
| 118 |
-
|
| 119 |
return {"story": ai_response}
|
| 120 |
|
| 121 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
|
|
| 89 |
# Add more language mappings as needed
|
| 90 |
}
|
| 91 |
|
| 92 |
+
from fastapi import Query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
@app.post("/generate-story/")
|
| 95 |
+
async def generate_story(image_file: UploadFile = File(...), language: str = Query("english")):
|
| 96 |
image_data = await image_file.read()
|
| 97 |
system_prompt = f"write an attractive story in 300 words about {random.choice(['an adventurous journey', 'a mysterious encounter', 'a heroic quest'])}"
|
| 98 |
caption = generate_image_caption(image_data)
|
| 99 |
if caption.startswith("Error"):
|
| 100 |
raise HTTPException(status_code=500, detail=caption)
|
| 101 |
ai_response = next(run(caption, [], system_prompt))
|
| 102 |
+
|
| 103 |
+
# Translate the generated story if language is Arabic
|
| 104 |
+
if language == "arabic":
|
| 105 |
+
translator_to_ar = GoogleTranslator(source='english', target='arabic')
|
| 106 |
+
ai_response = translator_to_ar.translate(ai_response)
|
| 107 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
return {"story": ai_response}
|
| 109 |
|
| 110 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|