Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import os
|
| 5 |
import uuid
|
| 6 |
import re
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
@@ -17,6 +18,22 @@ class RequestBody(BaseModel):
|
|
| 17 |
async def generate_response(request_body: RequestBody):
|
| 18 |
prompt = request_body.prompt
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
return {"response": all_chunk}
|
|
|
|
| 4 |
import os
|
| 5 |
import uuid
|
| 6 |
import re
|
| 7 |
+
from deep_translator import GoogleTranslator
|
| 8 |
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
|
|
| 18 |
async def generate_response(request_body: RequestBody):
|
| 19 |
prompt = request_body.prompt
|
| 20 |
|
| 21 |
+
def is_russian_word(word):
|
| 22 |
+
return all('а' <= char <= 'я' or 'А' <= char <= 'Я' for char in word)
|
| 23 |
+
|
| 24 |
+
def detect_russian_text(text):
|
| 25 |
+
words = re.findall(r'\b\w+\b', text)
|
| 26 |
+
if not words:
|
| 27 |
+
return False
|
| 28 |
+
russian_word_count = sum(1 for word in words if is_russian_word(word))
|
| 29 |
+
russian_ratio = russian_word_count / len(words)
|
| 30 |
+
return russian_ratio > 0.5
|
| 31 |
+
|
| 32 |
+
if detect_russian_text(prompt):
|
| 33 |
+
try:
|
| 34 |
+
all_chunk = GoogleTranslator(source='ru', target='en').translate(prompt)
|
| 35 |
+
|
| 36 |
+
except:
|
| 37 |
+
all_chunk = prompt
|
| 38 |
|
| 39 |
return {"response": all_chunk}
|