Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Request
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
from fastapi.responses import RedirectResponse, JSONResponse, HTMLResponse
|
|
@@ -60,7 +61,7 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
| 60 |
app.mount("/images", StaticFiles(directory="images"), name="images")
|
| 61 |
|
| 62 |
# Gemini API Configuration
|
| 63 |
-
API_KEY = "AIzaSyCwmgD8KxzWiuivtySNtcZF_rfTvx9s9sY"
|
| 64 |
genai.configure(api_key=API_KEY)
|
| 65 |
|
| 66 |
# Model configurations
|
|
@@ -192,21 +193,22 @@ def translate_text(text: str, target_language: str):
|
|
| 192 |
early_stopping=True
|
| 193 |
)
|
| 194 |
translated_text = translation_tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
| 195 |
-
logger
|
| 196 |
-
|
| 197 |
-
return translated_text
|
| 198 |
-
|
| 199 |
-
except Exception as e:
|
| 200 |
-
logger.error(f"Translation error: {str(e)}", exc_info=True)
|
| 201 |
-
return f"Translation error: {str(e)}"
|
| 202 |
|
| 203 |
def detect_intent(text: str = None, file: UploadFile = None) -> tuple[str, str]:
|
| 204 |
"""Enhanced intent detection with dynamic translation and file translation support"""
|
| 205 |
target_language = "English" # Default
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
if file and text:
|
| 208 |
text_lower = text.lower()
|
| 209 |
-
filename = file.filename.lower() if file.filename else ""
|
| 210 |
|
| 211 |
# Check for file translation intent
|
| 212 |
translate_patterns = [
|
|
@@ -223,7 +225,6 @@ def detect_intent(text: str = None, file: UploadFile = None) -> tuple[str, str]:
|
|
| 223 |
return "file-translate", target_language
|
| 224 |
|
| 225 |
# Image-related intents
|
| 226 |
-
content_type = file.content_type.lower() if file.content_type else ""
|
| 227 |
if content_type.startswith('image/') and text:
|
| 228 |
if "what’s this" in text_lower or "does this fly" in text_lower or ("fly" in text_lower and any(q in text_lower for q in ['does', 'can', 'will'])):
|
| 229 |
return "visual-qa", target_language
|
|
@@ -378,7 +379,7 @@ async def process_input(
|
|
| 378 |
|
| 379 |
content = await extract_text_from_file(file)
|
| 380 |
if not content.strip():
|
| 381 |
-
raise HTTPException(status_code=400, detail="
|
| 382 |
|
| 383 |
# Split content into chunks to handle large files
|
| 384 |
max_chunk_size = 512
|
|
|
|
| 1 |
+
|
| 2 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Request
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from fastapi.responses import RedirectResponse, JSONResponse, HTMLResponse
|
|
|
|
| 61 |
app.mount("/images", StaticFiles(directory="images"), name="images")
|
| 62 |
|
| 63 |
# Gemini API Configuration
|
| 64 |
+
API_KEY = "AIzaSyCwmgD8KxzWiuivtySNtcZF_rfTvx9s9sY" # Replace with your actual API key
|
| 65 |
genai.configure(api_key=API_KEY)
|
| 66 |
|
| 67 |
# Model configurations
|
|
|
|
| 193 |
early_stopping=True
|
| 194 |
)
|
| 195 |
translated_text = translation_tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
| 196 |
+
logger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
def detect_intent(text: str = None, file: UploadFile = None) -> tuple[str, str]:
|
| 199 |
"""Enhanced intent detection with dynamic translation and file translation support"""
|
| 200 |
target_language = "English" # Default
|
| 201 |
|
| 202 |
+
# Handle image captioning first: if a file is an image and no text is provided, assume image-to-text
|
| 203 |
+
if file:
|
| 204 |
+
content_type = file.content_type.lower() if file.content_type else ""
|
| 205 |
+
filename = file.filename.lower() if file.filename else ""
|
| 206 |
+
|
| 207 |
+
if content_type.startswith('image/') and not text:
|
| 208 |
+
return "image-to-text", target_language
|
| 209 |
+
|
| 210 |
if file and text:
|
| 211 |
text_lower = text.lower()
|
|
|
|
| 212 |
|
| 213 |
# Check for file translation intent
|
| 214 |
translate_patterns = [
|
|
|
|
| 225 |
return "file-translate", target_language
|
| 226 |
|
| 227 |
# Image-related intents
|
|
|
|
| 228 |
if content_type.startswith('image/') and text:
|
| 229 |
if "what’s this" in text_lower or "does this fly" in text_lower or ("fly" in text_lower and any(q in text_lower for q in ['does', 'can', 'will'])):
|
| 230 |
return "visual-qa", target_language
|
|
|
|
| 379 |
|
| 380 |
content = await extract_text_from_file(file)
|
| 381 |
if not content.strip():
|
| 382 |
+
raise HTTPException(status_code=400, detail=" voisins du texte could be extracted from the file")
|
| 383 |
|
| 384 |
# Split content into chunks to handle large files
|
| 385 |
max_chunk_size = 512
|