Update app.py
Browse files
app.py
CHANGED
|
@@ -1,357 +1,155 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
<meta charset="UTF-8">
|
| 5 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title>Translation API</title>
|
| 7 |
-
<link rel="stylesheet" href="style.css">
|
| 8 |
-
<style>
|
| 9 |
-
/* Add some basic styles for responsiveness and layout */
|
| 10 |
-
body {
|
| 11 |
-
font-family: sans-serif;
|
| 12 |
-
margin: 0;
|
| 13 |
-
padding: 20px;
|
| 14 |
-
background-color: #f4f4f4;
|
| 15 |
-
color: #333;
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
.container {
|
| 19 |
-
max-width: 800px;
|
| 20 |
-
margin: auto;
|
| 21 |
-
background-color: #fff;
|
| 22 |
-
padding: 20px;
|
| 23 |
-
border-radius: 8px;
|
| 24 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 25 |
-
}
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
label {
|
| 32 |
-
display: block;
|
| 33 |
-
margin-top: 1em;
|
| 34 |
-
font-weight: bold;
|
| 35 |
-
}
|
| 36 |
|
| 37 |
-
|
| 38 |
-
textarea,
|
| 39 |
-
select {
|
| 40 |
-
width: 100%;
|
| 41 |
-
padding: 8px;
|
| 42 |
-
margin-top: 5px;
|
| 43 |
-
border: 1px solid #ccc;
|
| 44 |
-
border-radius: 4px;
|
| 45 |
-
box-sizing: border-box; /* Include padding and border in the element's total width and height */
|
| 46 |
-
}
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
padding: 10px 15px;
|
| 52 |
-
border: none;
|
| 53 |
-
border-radius: 4px;
|
| 54 |
-
cursor: pointer;
|
| 55 |
-
margin-top: 1em;
|
| 56 |
-
}
|
| 57 |
|
| 58 |
-
|
| 59 |
-
background-color: #0056b3;
|
| 60 |
-
}
|
| 61 |
|
| 62 |
-
#result {
|
| 63 |
-
margin-top: 2em;
|
| 64 |
-
padding: 1em;
|
| 65 |
-
border: 1px solid #ddd;
|
| 66 |
-
background-color: #f9f9f9;
|
| 67 |
-
border-radius: 4px;
|
| 68 |
-
display: none; /* Hide initially */
|
| 69 |
-
}
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
.loading-indicator {
|
| 77 |
-
display: none; /* Initially hide the loading indicator */
|
| 78 |
-
margin-top: 1em;
|
| 79 |
-
color: #007bff;
|
| 80 |
-
}
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
.tab button {
|
| 91 |
-
background-color: inherit;
|
| 92 |
-
float: left;
|
| 93 |
-
border: none;
|
| 94 |
-
outline: none;
|
| 95 |
-
cursor: pointer;
|
| 96 |
-
padding: 14px 16px;
|
| 97 |
-
transition: 0.3s;
|
| 98 |
-
font-size: 17px;
|
| 99 |
-
border-right: 1px solid #ccc; /* Add a separator between buttons */
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
.tab button:last-child {
|
| 103 |
-
border-right: none; /* Remove border from the last button */
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
.tab button:hover {
|
| 107 |
-
background-color: #ddd;
|
| 108 |
-
}
|
| 109 |
-
|
| 110 |
-
.tab button.active {
|
| 111 |
-
background-color: #ccc;
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
.tabcontent {
|
| 115 |
-
display: none;
|
| 116 |
-
padding: 6px 12px;
|
| 117 |
-
border: 1px solid #ccc;
|
| 118 |
-
border-top: none;
|
| 119 |
-
border-radius: 0 0 4px 4px; /* Rounded bottom corners */
|
| 120 |
-
}
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
}
|
| 126 |
-
|
| 127 |
-
</style>
|
| 128 |
-
</head>
|
| 129 |
-
<body>
|
| 130 |
-
<div class="container">
|
| 131 |
-
<h1>Translation and Language Detection API</h1>
|
| 132 |
-
|
| 133 |
-
<!-- Tabs -->
|
| 134 |
-
<div class="tab">
|
| 135 |
-
<button class="tablinks active" onclick="openTab(event, 'translate')">Translate</button>
|
| 136 |
-
<button class="tablinks" onclick="openTab(event, 'detect')">Detect Language</button>
|
| 137 |
-
</div>
|
| 138 |
-
|
| 139 |
-
<!-- Translation Tab Content -->
|
| 140 |
-
<div id="translate" class="tabcontent" style="display: block;"> <!-- translate tab set display block -->
|
| 141 |
-
<h2>Translate Text</h2>
|
| 142 |
-
<label for="text-to-translate">Text to Translate:</label>
|
| 143 |
-
<textarea id="text-to-translate" rows="4" placeholder="Enter text here..."></textarea>
|
| 144 |
-
|
| 145 |
-
<label for="source-language">Source Language (optional, auto-detect if empty):</label>
|
| 146 |
-
<input type="text" id="source-language" placeholder="e.g., English, en, auto">
|
| 147 |
-
|
| 148 |
-
<label for="target-language">Target Language:</label>
|
| 149 |
-
<input type="text" id="target-language" placeholder="e.g., Spanish, es">
|
| 150 |
-
|
| 151 |
-
<button id="translate-button">Translate</button>
|
| 152 |
-
|
| 153 |
-
<div class="loading-indicator" id="translation-loading">Loading...</div>
|
| 154 |
-
|
| 155 |
-
<div id="translation-result">
|
| 156 |
-
<h3>Translation Result:</h3>
|
| 157 |
-
<p><strong>Source Language:</strong> <span id="detected-source-language"></span></p>
|
| 158 |
-
<p><strong>Target Language:</strong> <span id="translation-target-language"></span></p>
|
| 159 |
-
<p><strong>Translated Text:</strong> <span id="translated-text"></span></p>
|
| 160 |
-
</div>
|
| 161 |
-
|
| 162 |
-
</div> <!-- end of translation tab -->
|
| 163 |
-
|
| 164 |
-
<!-- Detect Language Tab Content -->
|
| 165 |
-
<div id="detect" class="tabcontent">
|
| 166 |
-
<h2>Detect Language</h2>
|
| 167 |
-
<label for="text-to-detect">Text to Detect:</label>
|
| 168 |
-
<textarea id="text-to-detect" rows="4" placeholder="Enter text here..."></textarea>
|
| 169 |
-
<button id="detect-button">Detect Language</button>
|
| 170 |
-
|
| 171 |
-
<div class="loading-indicator" id="detection-loading">Loading...</div>
|
| 172 |
-
|
| 173 |
-
<div id="detection-result">
|
| 174 |
-
<h3>Detection Result:</h3>
|
| 175 |
-
<p><strong>Detected Language:</strong> <span id="detected-language"></span></p>
|
| 176 |
-
<div id="translation-options-container">
|
| 177 |
-
<h4>Translation Options:</h4>
|
| 178 |
-
<ul id="translation-options-list"></ul>
|
| 179 |
-
</div>
|
| 180 |
-
</div>
|
| 181 |
-
|
| 182 |
-
</div> <!-- end of language detection tab-->
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
<div id="error-message"></div>
|
| 186 |
-
|
| 187 |
-
</div>
|
| 188 |
-
|
| 189 |
-
<script>
|
| 190 |
-
// --- API Base URL (Replace with your actual base URL) ---
|
| 191 |
-
const API_BASE_URL = 'https://sameernotes-translation-prediction-space.hf.space'; // Replace with your actual hosted URL or localhost
|
| 192 |
-
const supportedLanguages = ["Afrikaans","Arabic","Armenian","Azerbaijani","Belarusian","Bosnian","Bulgarian","Catalan","Chinese","Croatian","Czech","Danish","Dutch","English","Estonian","Finnish","French","Galician","German","Greek","Hebrew","Hindi","Hungarian","Icelandic","Indonesian","Italian","Japanese","Kannada","Kazakh","Korean","Latvian","Lithuanian","Macedonian","Malay","Marathi","Maori","Nepali","Norwegian","Persian","Polish","Portuguese","Romanian","Russian","Serbian","Slovak","Slovenian","Spanish","Swahili","Swedish","Tagalog","Tamil","Thai","Turkish","Ukrainian","Urdu","Vietnamese","Welsh",]; // Add all supported languages
|
| 193 |
-
|
| 194 |
-
// --- Helper Functions ---
|
| 195 |
-
|
| 196 |
-
// Function to show error messages.
|
| 197 |
-
function showError(message) {
|
| 198 |
-
document.getElementById('error-message').textContent = message;
|
| 199 |
-
document.getElementById('error-message').style.display = 'block';
|
| 200 |
-
}
|
| 201 |
-
// Function to clear error messages.
|
| 202 |
-
function clearError() {
|
| 203 |
-
document.getElementById('error-message').textContent = '';
|
| 204 |
-
document.getElementById('error-message').style.display = 'none';
|
| 205 |
-
}
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
tablinks = document.getElementsByClassName("tablinks");
|
| 215 |
-
for (i = 0; i < tablinks.length; i++) {
|
| 216 |
-
tablinks[i].className = tablinks[i].className.replace(" active", "");
|
| 217 |
-
}
|
| 218 |
-
document.getElementById(tabName).style.display = "block";
|
| 219 |
-
evt.currentTarget.className += " active";
|
| 220 |
-
|
| 221 |
-
// Hide results when switching tabs
|
| 222 |
-
document.getElementById('translation-result').style.display = 'none';
|
| 223 |
-
document.getElementById('detection-result').style.display = 'none';
|
| 224 |
}
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
return
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
}
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
optionsList.appendChild(listItem);
|
| 338 |
-
}
|
| 339 |
-
document.getElementById('translation-options-container').style.display = 'block'; // Show the container
|
| 340 |
-
} else {
|
| 341 |
-
document.getElementById('translation-options-container').style.display = 'none'; // Hide if no options
|
| 342 |
-
}
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
document.getElementById('detection-result').style.display = 'block';
|
| 346 |
-
|
| 347 |
-
} catch (error) {
|
| 348 |
-
showError(`Error: ${error.message}`);
|
| 349 |
-
console.error('Error during language detection:', error);
|
| 350 |
-
} finally {
|
| 351 |
-
document.getElementById('detection-loading').style.display = 'none';
|
| 352 |
-
}
|
| 353 |
-
});
|
| 354 |
-
|
| 355 |
-
</script>
|
| 356 |
-
</body>
|
| 357 |
-
</html>
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re # Import the regular expression module
|
| 3 |
+
from typing import List
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
from fastapi import FastAPI, HTTPException, status
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
import requests
|
| 8 |
+
from fastapi.responses import HTMLResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 13 |
+
if not GEMINI_API_KEY:
|
| 14 |
+
raise ValueError("The GEMINI_API_KEY environment variable is not set.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-lite:generateContent?key=" + GEMINI_API_KEY
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
class TranslationRequest(BaseModel):
|
| 20 |
+
text: str
|
| 21 |
+
target_language: str
|
| 22 |
+
source_language: str = None
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
class TranslationResponse(BaseModel): # For /detect_language
|
| 26 |
+
translated_text: str
|
| 27 |
+
source_language: str
|
| 28 |
+
target_language: str
|
| 29 |
+
|
| 30 |
+
# --- Helper Functions ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def detect_language_and_options(text: str):
|
| 33 |
+
"""Detects the language and provides translation options."""
|
| 34 |
+
prompt = f"""Please identify the language of the text provided and then offer translation options as numbered choices (1-5). Use this format: "The text is in [Language]. Choose a language to translate to: 1. [Option 1], 2. [Option 2], 3. [Option 3], 4. [Option 4], 5. [Option 5]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
Input Text: {text}"""
|
| 37 |
+
|
| 38 |
+
request_data = {
|
| 39 |
+
"contents": [{
|
| 40 |
+
"role": "user",
|
| 41 |
+
"parts": [{"text": prompt}]
|
| 42 |
+
}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
+
try:
|
| 46 |
+
response = requests.post(GEMINI_API_URL, json=request_data)
|
| 47 |
+
response.raise_for_status()
|
| 48 |
+
response_json = response.json()
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
response_text = response_json['candidates'][0]['content']['parts'][0]['text']
|
| 52 |
+
source_language = response_text.split("The text is in ")[1].split(".")[0].strip()
|
| 53 |
+
options_str = response_text.split("Choose a language to translate to:")[1].strip()
|
| 54 |
+
options_list = [opt.split(". ")[1].strip() for opt in options_str.split(", ")]
|
| 55 |
+
while len(options_list) < 5:
|
| 56 |
+
options_list.append("Option Not Available")
|
| 57 |
+
options_list = options_list[:5]
|
| 58 |
+
options = {str(i + 1): lang for i, lang in enumerate(options_list)}
|
| 59 |
+
return source_language, options
|
| 60 |
+
except (KeyError, IndexError, AttributeError) as e:
|
| 61 |
+
raise HTTPException(status_code=500, detail=f"Error parsing Gemini API response: {e}")
|
| 62 |
+
except requests.exceptions.RequestException as e:
|
| 63 |
+
raise HTTPException(status_code=500, detail=f"Error communicating with Gemini API: {e}")
|
| 64 |
+
except Exception as e:
|
| 65 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def translate_with_gemini(text: str, source_language: str, target_language: str) -> str:
|
| 69 |
+
"""Translates text, requesting HTML, and removes Markdown code blocks."""
|
| 70 |
+
|
| 71 |
+
prompt = f"""Translate the following text from {source_language} to {target_language}. Return *only* the translated text. Format the response as HTML. If any part of the translated text should be emphasized (like an example or key phrase), use `<strong>` tags around that part. Do not include ANY additional text like 'Here is the translation:' or similar. Only the translated text, formatted as HTML.
|
| 72 |
+
|
| 73 |
+
Text:
|
| 74 |
+
{text}"""
|
| 75 |
+
|
| 76 |
+
request_data = {
|
| 77 |
+
"contents": [{
|
| 78 |
+
"role": "user",
|
| 79 |
+
"parts": [{"text": prompt}]
|
| 80 |
+
}]
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
response = requests.post(GEMINI_API_URL, json=request_data)
|
| 85 |
+
response.raise_for_status()
|
| 86 |
+
response_json = response.json()
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
translated_text = response_json['candidates'][0]['content']['parts'][0]['text']
|
| 90 |
+
|
| 91 |
+
# Remove Markdown code blocks (```html ... ```)
|
| 92 |
+
translated_text = re.sub(r"```html\n?([\s\S]*?)\n?```", r"\1", translated_text).strip()
|
| 93 |
+
translated_text = re.sub(r"```([\s\S]*?)```", r"\1", translated_text).strip() # Remove plain ``` ... ```
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
return translated_text
|
| 97 |
+
except (KeyError, IndexError) as e:
|
| 98 |
+
raise HTTPException(status_code=500, detail=f"Error parsing Gemini API response: {e}")
|
| 99 |
+
except requests.exceptions.RequestException as e:
|
| 100 |
+
raise HTTPException(status_code=500, detail=f"Error communicating with Gemini API: {e}")
|
| 101 |
+
except Exception as e:
|
| 102 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
@app.post("/translate", response_class=HTMLResponse, status_code=status.HTTP_200_OK)
|
| 106 |
+
async def translate(request: TranslationRequest):
|
| 107 |
+
"""Translates text, returning HTML."""
|
| 108 |
+
|
| 109 |
+
if not request.text:
|
| 110 |
+
raise HTTPException(status_code=400, detail="Text to translate cannot be empty.")
|
| 111 |
+
if not request.target_language:
|
| 112 |
+
raise HTTPException(status_code=400, detail="Target language must be provided.")
|
| 113 |
+
|
| 114 |
+
if request.source_language:
|
| 115 |
+
source_language = request.source_language
|
| 116 |
+
else:
|
| 117 |
+
try:
|
| 118 |
+
source_language, _ = detect_language_and_options(request.text)
|
| 119 |
+
except HTTPException as e:
|
| 120 |
+
raise e
|
| 121 |
+
|
| 122 |
+
supported_languages = ["Afrikaans", "Arabic", "Armenian", "Azerbaijani", "Belarusian", "Bosnian", "Bulgarian",
|
| 123 |
+
"Catalan", "Chinese", "Croatian", "Czech", "Danish", "Dutch", "English", "Estonian",
|
| 124 |
+
"Finnish", "French", "Galician", "German", "Greek", "Hebrew", "Hindi", "Hungarian",
|
| 125 |
+
"Icelandic", "Indonesian", "Italian", "Japanese", "Kannada", "Kazakh", "Korean", "Latvian",
|
| 126 |
+
"Lithuanian", "Macedonian", "Malay", "Marathi", "Maori", "Nepali", "Norwegian", "Persian",
|
| 127 |
+
"Polish", "Portuguese", "Romanian", "Russian", "Serbian", "Slovak", "Slovenian", "Spanish",
|
| 128 |
+
"Swahili", "Swedish", "Tagalog", "Tamil", "Thai", "Turkish", "Ukrainian", "Urdu",
|
| 129 |
+
"Vietnamese", "Welsh"]
|
| 130 |
+
|
| 131 |
+
if request.target_language not in supported_languages:
|
| 132 |
+
raise HTTPException(status_code=400, detail=f"Target language '{request.target_language}' is not supported.")
|
| 133 |
+
|
| 134 |
+
try:
|
| 135 |
+
translated_html = translate_with_gemini(request.text, source_language, request.target_language)
|
| 136 |
+
return translated_html
|
| 137 |
+
except HTTPException as e:
|
| 138 |
+
raise e
|
| 139 |
+
except Exception as e:
|
| 140 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
@app.post("/detect_language", status_code=status.HTTP_200_OK)
|
| 145 |
+
async def detect_language(text: str = ""):
|
| 146 |
+
"""Detects the language of the input text and provides translation options."""
|
| 147 |
+
if not text:
|
| 148 |
+
raise HTTPException(status_code=400, detail="Text to detect cannot be empty.")
|
| 149 |
+
try:
|
| 150 |
+
source_language, options = detect_language_and_options(text)
|
| 151 |
+
return {"source_language": source_language, "translation_options": options}
|
| 152 |
+
except HTTPException as e:
|
| 153 |
+
raise e
|
| 154 |
+
except Exception as e:
|
| 155 |
+
raise HTTPException(status_code=500,detail=f"An unexpected error occurred {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|