Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,6 +49,27 @@ def safe_text(text: str) -> str:
|
|
| 49 |
text = str(text).strip()
|
| 50 |
text = re.sub(r"\s+", " ", text)
|
| 51 |
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def transcribe_audio_file(filepath: str) -> str:
|
| 54 |
try:
|
|
@@ -98,6 +119,10 @@ def build_system_prompt():
|
|
| 98 |
|
| 99 |
def generate_letter_with_llm(raw_text: str, picked_authority: str, user_info: dict):
|
| 100 |
raw_text = safe_text(raw_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
if OPENAI_KEY:
|
| 102 |
try:
|
| 103 |
system_prompt = build_system_prompt()
|
|
|
|
| 49 |
text = str(text).strip()
|
| 50 |
text = re.sub(r"\s+", " ", text)
|
| 51 |
return text
|
| 52 |
+
# Check if text contains Urdu characters
|
| 53 |
+
def is_urdu(text):
|
| 54 |
+
return any("\u0600" <= c <= "\u06FF" for c in text)
|
| 55 |
+
|
| 56 |
+
# Translate Urdu to English using OpenAI
|
| 57 |
+
def translate_ur_to_en(urdu_text):
|
| 58 |
+
if not urdu_text.strip():
|
| 59 |
+
return ""
|
| 60 |
+
if OPENAI_KEY:
|
| 61 |
+
try:
|
| 62 |
+
prompt = f"Translate the following Urdu text to English:\n{urdu_text}"
|
| 63 |
+
resp = openai.ChatCompletion.create(
|
| 64 |
+
model="gpt-4o-mini",
|
| 65 |
+
messages=[{"role":"user","content":prompt}],
|
| 66 |
+
temperature=0
|
| 67 |
+
)
|
| 68 |
+
return resp['choices'][0]['message']['content'].strip()
|
| 69 |
+
except Exception:
|
| 70 |
+
return urdu_text # fallback: return original text if translation fails
|
| 71 |
+
return urdu_text
|
| 72 |
+
|
| 73 |
|
| 74 |
def transcribe_audio_file(filepath: str) -> str:
|
| 75 |
try:
|
|
|
|
| 119 |
|
| 120 |
def generate_letter_with_llm(raw_text: str, picked_authority: str, user_info: dict):
|
| 121 |
raw_text = safe_text(raw_text)
|
| 122 |
+
# Auto-translate if text is in Urdu
|
| 123 |
+
if is_urdu(raw_text):
|
| 124 |
+
raw_text = translate_ur_to_en(raw_text)
|
| 125 |
+
|
| 126 |
if OPENAI_KEY:
|
| 127 |
try:
|
| 128 |
system_prompt = build_system_prompt()
|