Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
main.py
CHANGED
@@ -4,7 +4,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
import json
|
7 |
-
from utils import get_text
|
8 |
from json_flatten import flatten
|
9 |
|
10 |
app = FastAPI(
|
@@ -107,3 +107,27 @@ async def list_models():
|
|
107 |
]
|
108 |
|
109 |
return JSONResponse(content={"models": models})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
import json
|
7 |
+
from utils import get_text, translate_text
|
8 |
from json_flatten import flatten
|
9 |
|
10 |
app = FastAPI(
|
|
|
107 |
]
|
108 |
|
109 |
return JSONResponse(content={"models": models})
|
110 |
+
|
111 |
+
|
112 |
+
@app.get("/translate")
|
113 |
+
async def translate_text_endpoint(text: str, target_language: str):
|
114 |
+
"""
|
115 |
+
### Endpoint Description:
|
116 |
+
Translate text to a specified language.
|
117 |
+
|
118 |
+
#### Request Parameters:
|
119 |
+
- `text`: The text to translate. (Required)
|
120 |
+
- `target_language`: The language to translate the text to. (Required)
|
121 |
+
|
122 |
+
#### Response:
|
123 |
+
- The translated text in the specified language.
|
124 |
+
"""
|
125 |
+
|
126 |
+
try:
|
127 |
+
# print(target_language)
|
128 |
+
response = translate_text(text, target_language)
|
129 |
+
# print(response) #{"translated_text":"नमस्ते","translated_language":"नेपाली"}
|
130 |
+
response = json.loads(response)
|
131 |
+
return JSONResponse(content=response)
|
132 |
+
except Exception as e:
|
133 |
+
return JSONResponse(content={"error": str(e)}, status_code=400)
|
models.py
CHANGED
@@ -5,3 +5,7 @@ from typing import Optional, List
|
|
5 |
class FormDetails(BaseModel):
|
6 |
fields: List[str]
|
7 |
values: List[str]
|
|
|
|
|
|
|
|
|
|
5 |
class FormDetails(BaseModel):
|
6 |
fields: List[str]
|
7 |
values: List[str]
|
8 |
+
|
9 |
+
class TranslateDetails(BaseModel):
|
10 |
+
translated_text: str
|
11 |
+
translated_language: str
|
prompts.py
CHANGED
@@ -25,3 +25,17 @@ prompt = """Please extract the [{}] details from this image, and then output int
|
|
25 |
|
26 |
# # Print or use the resulting system_prompt
|
27 |
# print(system_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# # Print or use the resulting system_prompt
|
27 |
# print(system_prompt)
|
28 |
+
|
29 |
+
translate_system_prompt = """
|
30 |
+
You are a translation tool that translates the following text into [{}] language.
|
31 |
+
1. Please translate the text into the language specified.
|
32 |
+
2. The text may contain technical terms, proper nouns, and other specialized vocabulary.
|
33 |
+
3. The text may contain multiple sentences.
|
34 |
+
4. The text may contain spelling errors.
|
35 |
+
5. The text may contain grammatical errors.
|
36 |
+
6. The text may contain punctuation errors.
|
37 |
+
7. The text may contain formatting errors.
|
38 |
+
8. The output should be like [{}] formate json.
|
39 |
+
"""
|
40 |
+
|
41 |
+
translation_prompt = """Please translate the following into [{}] language :- \n\n [{}] ."""
|
utils.py
CHANGED
@@ -2,8 +2,8 @@ from dotenv import load_dotenv
|
|
2 |
import os
|
3 |
# import openai
|
4 |
from openai import OpenAI
|
5 |
-
from models import FormDetails
|
6 |
-
from prompts import system_prompt_template, prompt
|
7 |
import base64
|
8 |
from io import BytesIO
|
9 |
import anthropic
|
@@ -145,4 +145,28 @@ def get_text(image, filename, model, fields="ALL"):
|
|
145 |
)
|
146 |
response = response.choices[0].message.content
|
147 |
# print(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
return response
|
|
|
2 |
import os
|
3 |
# import openai
|
4 |
from openai import OpenAI
|
5 |
+
from models import FormDetails, TranslateDetails
|
6 |
+
from prompts import system_prompt_template, prompt, translate_system_prompt, translation_prompt
|
7 |
import base64
|
8 |
from io import BytesIO
|
9 |
import anthropic
|
|
|
145 |
)
|
146 |
response = response.choices[0].message.content
|
147 |
# print(response)
|
148 |
+
return response
|
149 |
+
|
150 |
+
def translate_text(text, target_language):
|
151 |
+
# print("translating")
|
152 |
+
client = OpenAI(api_key = OPENAI_API_KEY)
|
153 |
+
# print("hi")
|
154 |
+
response = client.beta.chat.completions.parse(
|
155 |
+
model="gpt-4o",
|
156 |
+
messages=[
|
157 |
+
{
|
158 |
+
"role":"system",
|
159 |
+
"content":translate_system_prompt.format(target_language,TranslateDetails.schema_json())
|
160 |
+
},
|
161 |
+
{
|
162 |
+
"role": "user",
|
163 |
+
"content": translation_prompt.format(target_language,text)
|
164 |
+
},
|
165 |
+
],
|
166 |
+
|
167 |
+
response_format=TranslateDetails,
|
168 |
+
temperature=0.0,
|
169 |
+
)
|
170 |
+
# print(response.choices[0].message.content)
|
171 |
+
response = response.choices[0].message.content
|
172 |
return response
|