Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ def get_groq_response(prompt):
|
|
| 24 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 25 |
chat_completion = client.chat.completions.create(
|
| 26 |
messages=[{"role": "user", "content": prompt}],
|
| 27 |
-
model="
|
| 28 |
)
|
| 29 |
return chat_completion.choices[0].message.content
|
| 30 |
|
|
@@ -48,26 +48,6 @@ def generate_script(topic, duration):
|
|
| 48 |
return ""
|
| 49 |
|
| 50 |
|
| 51 |
-
def translate_to_urdu(english_script):
|
| 52 |
-
try:
|
| 53 |
-
tokenizer.src_lang = "en"
|
| 54 |
-
inputs = tokenizer(english_script, return_tensors="pt", truncation=True, max_length=1024).to(model.device)
|
| 55 |
-
|
| 56 |
-
translated_tokens = model.generate(
|
| 57 |
-
**inputs,
|
| 58 |
-
max_length=1024,
|
| 59 |
-
no_repeat_ngram_size=2, # Reduce repetition checks (faster)
|
| 60 |
-
forced_bos_token_id=tokenizer.get_lang_id("ur"),
|
| 61 |
-
num_beams=2 # Faster than beam search with high values
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
urdu_script = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 65 |
-
return urdu_script
|
| 66 |
-
|
| 67 |
-
except Exception as e:
|
| 68 |
-
return f"❌ Error in translation: {str(e)}"
|
| 69 |
-
|
| 70 |
-
# # ✅ Function to Translate English Script to Urdu
|
| 71 |
# def translate_to_urdu(english_script):
|
| 72 |
# try:
|
| 73 |
# tokenizer.src_lang = "en"
|
|
@@ -77,38 +57,70 @@ def translate_to_urdu(english_script):
|
|
| 77 |
# **inputs,
|
| 78 |
# max_length=1024,
|
| 79 |
# no_repeat_ngram_size=2, # Reduce repetition checks (faster)
|
| 80 |
-
# forced_bos_token_id=tokenizer.
|
| 81 |
-
# num_beams=2 # Faster
|
| 82 |
# )
|
| 83 |
|
| 84 |
# urdu_script = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 85 |
# return urdu_script
|
|
|
|
| 86 |
# except Exception as e:
|
| 87 |
# return f"❌ Error in translation: {str(e)}"
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
-
# ✅ Function to Save Edited Urdu Script to a File
|
| 92 |
def save_edited_urdu(edited_urdu_script, topic):
|
| 93 |
timestamp = datetime.datetime.now().strftime("%Y_%m_%d")
|
| 94 |
filename = f"{topic}_Urdu_Final_{timestamp}.txt"
|
|
|
|
| 95 |
|
| 96 |
-
with open(
|
| 97 |
file.write(edited_urdu_script)
|
| 98 |
|
| 99 |
-
return
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
def save_file(content, filename):
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
file.write(content)
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
|
| 109 |
def process_request(topic, duration):
|
| 110 |
eng_script = generate_script(topic, duration)
|
| 111 |
-
return eng_script, ""
|
|
|
|
| 112 |
|
| 113 |
def process_translation(eng_script):
|
| 114 |
urdu_script = translate_to_urdu(eng_script)
|
|
@@ -137,10 +149,13 @@ with gr.Blocks() as app:
|
|
| 137 |
duration_input = gr.Slider(minimum=1, maximum=30, step=1, label="Duration (minutes)")
|
| 138 |
generate_button = gr.Button("Generate English Script")
|
| 139 |
eng_output = gr.Textbox(label="Generated English Script", interactive=False)
|
| 140 |
-
generate_button.click(
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
| 142 |
translate_button = gr.Button("Generate Urdu Script")
|
| 143 |
-
urdu_output = gr.Textbox(label="Translated Urdu Script
|
| 144 |
translate_button.click(process_translation, inputs=[eng_output], outputs=[urdu_output])
|
| 145 |
|
| 146 |
# Editable Urdu Textbox
|
|
@@ -169,6 +184,10 @@ with gr.Blocks() as app:
|
|
| 169 |
|
| 170 |
download_eng_button.click(download_english, inputs=[eng_output, topic_input], outputs=[download_eng_file])
|
| 171 |
download_urdu_button.click(download_urdu, inputs=[urdu_output, topic_input], outputs=[download_urdu_file])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
download_final_urdu_button.click(download_final_urdu, inputs=[editable_urdu, topic_input], outputs=[download_final_urdu_file])
|
| 173 |
|
| 174 |
-
app.launch()
|
|
|
|
| 24 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 25 |
chat_completion = client.chat.completions.create(
|
| 26 |
messages=[{"role": "user", "content": prompt}],
|
| 27 |
+
model="llama3-70b-8192"
|
| 28 |
)
|
| 29 |
return chat_completion.choices[0].message.content
|
| 30 |
|
|
|
|
| 48 |
return ""
|
| 49 |
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# def translate_to_urdu(english_script):
|
| 52 |
# try:
|
| 53 |
# tokenizer.src_lang = "en"
|
|
|
|
| 57 |
# **inputs,
|
| 58 |
# max_length=1024,
|
| 59 |
# no_repeat_ngram_size=2, # Reduce repetition checks (faster)
|
| 60 |
+
# forced_bos_token_id=tokenizer.get_lang_id("ur"),
|
| 61 |
+
# num_beams=2 # Faster than beam search with high values
|
| 62 |
# )
|
| 63 |
|
| 64 |
# urdu_script = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 65 |
# return urdu_script
|
| 66 |
+
|
| 67 |
# except Exception as e:
|
| 68 |
# return f"❌ Error in translation: {str(e)}"
|
| 69 |
|
| 70 |
+
def translate_to_urdu(english_script):
|
| 71 |
+
try:
|
| 72 |
+
tokenizer.src_lang = "en"
|
| 73 |
+
max_length = 500 # Process smaller chunks to avoid truncation
|
| 74 |
+
input_chunks = [english_script[i:i+max_length] for i in range(0, len(english_script), max_length)]
|
| 75 |
+
|
| 76 |
+
translated_chunks = []
|
| 77 |
+
for chunk in input_chunks:
|
| 78 |
+
inputs = tokenizer(chunk, return_tensors="pt", truncation=True, max_length=1024).to(model.device)
|
| 79 |
+
translated_tokens = model.generate(
|
| 80 |
+
**inputs,
|
| 81 |
+
max_length=1024,
|
| 82 |
+
no_repeat_ngram_size=2,
|
| 83 |
+
forced_bos_token_id=tokenizer.get_lang_id("ur"),
|
| 84 |
+
num_beams=2
|
| 85 |
+
)
|
| 86 |
+
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 87 |
+
translated_chunks.append(translated_text)
|
| 88 |
+
|
| 89 |
+
return " ".join(translated_chunks)
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"❌ Error in translation: {str(e)}"
|
| 93 |
+
|
| 94 |
|
| 95 |
|
|
|
|
| 96 |
def save_edited_urdu(edited_urdu_script, topic):
|
| 97 |
timestamp = datetime.datetime.now().strftime("%Y_%m_%d")
|
| 98 |
filename = f"{topic}_Urdu_Final_{timestamp}.txt"
|
| 99 |
+
filepath = os.path.join(os.getcwd(), filename) # Ensure full path
|
| 100 |
|
| 101 |
+
with open(filepath, "w", encoding="utf-8") as file:
|
| 102 |
file.write(edited_urdu_script)
|
| 103 |
|
| 104 |
+
return filepath # Return full path for Gradio download
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
def save_file(content, filename):
|
| 110 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") # Add seconds to avoid overwriting
|
| 111 |
+
filename = f"{filename}_{timestamp}.txt" # Ensure uniqueness
|
| 112 |
+
filepath = os.path.join(os.getcwd(), filename)
|
| 113 |
+
|
| 114 |
+
with open(filepath, "w", encoding="utf-8") as file:
|
| 115 |
file.write(content)
|
| 116 |
+
|
| 117 |
+
return filepath
|
| 118 |
+
|
| 119 |
|
| 120 |
def process_request(topic, duration):
|
| 121 |
eng_script = generate_script(topic, duration)
|
| 122 |
+
return eng_script, "", "" # Clears Urdu and Edited Urdu textboxes
|
| 123 |
+
|
| 124 |
|
| 125 |
def process_translation(eng_script):
|
| 126 |
urdu_script = translate_to_urdu(eng_script)
|
|
|
|
| 149 |
duration_input = gr.Slider(minimum=1, maximum=30, step=1, label="Duration (minutes)")
|
| 150 |
generate_button = gr.Button("Generate English Script")
|
| 151 |
eng_output = gr.Textbox(label="Generated English Script", interactive=False)
|
| 152 |
+
generate_button.click(
|
| 153 |
+
process_request,
|
| 154 |
+
inputs=[topic_input, duration_input],
|
| 155 |
+
outputs=[eng_output, urdu_output, editable_urdu] # Clears Urdu & Edited Urdu textboxes
|
| 156 |
+
)
|
| 157 |
translate_button = gr.Button("Generate Urdu Script")
|
| 158 |
+
urdu_output = gr.Textbox(label="Translated Urdu Script", interactive=True)
|
| 159 |
translate_button.click(process_translation, inputs=[eng_output], outputs=[urdu_output])
|
| 160 |
|
| 161 |
# Editable Urdu Textbox
|
|
|
|
| 184 |
|
| 185 |
download_eng_button.click(download_english, inputs=[eng_output, topic_input], outputs=[download_eng_file])
|
| 186 |
download_urdu_button.click(download_urdu, inputs=[urdu_output, topic_input], outputs=[download_urdu_file])
|
| 187 |
+
# Save the edited script
|
| 188 |
+
save_button.click(save_edited_urdu, inputs=[editable_urdu, topic_input], outputs=[download_final_urdu_file])
|
| 189 |
+
|
| 190 |
+
|
| 191 |
download_final_urdu_button.click(download_final_urdu, inputs=[editable_urdu, topic_input], outputs=[download_final_urdu_file])
|
| 192 |
|
| 193 |
+
app.launch()
|