Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,72 +5,36 @@ from groq import Groq
|
|
| 5 |
import torch
|
| 6 |
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
| 7 |
|
| 8 |
-
|
| 9 |
# β
Set API Key
|
| 10 |
-
os.environ["GROQ_API_KEY"] = "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"
|
| 11 |
-
# GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 12 |
-
|
| 13 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 14 |
|
| 15 |
-
|
| 16 |
# β
Load M2M-100 Model
|
| 17 |
model_name = "facebook/m2m100_418M"
|
| 18 |
tokenizer = M2M100Tokenizer.from_pretrained(model_name)
|
| 19 |
model = M2M100ForConditionalGeneration.from_pretrained(model_name)
|
| 20 |
|
| 21 |
-
# β
Function to
|
| 22 |
-
def get_groq_response(prompt):
|
| 23 |
-
print("π€ Generating Response...")
|
| 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 |
-
|
| 31 |
def generate_script(topic, duration):
|
| 32 |
try:
|
| 33 |
-
words_per_minute = 130
|
| 34 |
target_words = duration * words_per_minute
|
| 35 |
|
| 36 |
response = client.chat.completions.create(
|
| 37 |
-
messages=[
|
| 38 |
-
{"role": "user", "content": f"Generate a plain, well-structured educational script about {topic} in English with approximately {target_words} words. Do not include stage directions, sound cues, or visual elementsβonly the spoken content."}
|
| 39 |
-
],
|
| 40 |
model="llama-3.3-70b-versatile"
|
| 41 |
)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
english_script = response.choices[0].message.content
|
| 45 |
-
return english_script
|
| 46 |
except Exception as e:
|
| 47 |
-
|
| 48 |
-
|
| 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 |
def translate_to_urdu(english_script):
|
| 71 |
try:
|
| 72 |
tokenizer.src_lang = "en"
|
| 73 |
-
max_length = 500
|
| 74 |
input_chunks = [english_script[i:i+max_length] for i in range(0, len(english_script), max_length)]
|
| 75 |
|
| 76 |
translated_chunks = []
|
|
@@ -83,32 +47,27 @@ def translate_to_urdu(english_script):
|
|
| 83 |
forced_bos_token_id=tokenizer.get_lang_id("ur"),
|
| 84 |
num_beams=2
|
| 85 |
)
|
| 86 |
-
|
| 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)
|
| 100 |
|
| 101 |
with open(filepath, "w", encoding="utf-8") as file:
|
| 102 |
file.write(edited_urdu_script)
|
| 103 |
|
| 104 |
-
return filepath
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
|
|
|
|
| 109 |
def save_file(content, filename):
|
| 110 |
-
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 111 |
-
filename = f"{filename}_{timestamp}.txt"
|
| 112 |
filepath = os.path.join(os.getcwd(), filename)
|
| 113 |
|
| 114 |
with open(filepath, "w", encoding="utf-8") as file:
|
|
@@ -116,30 +75,23 @@ def save_file(content, filename):
|
|
| 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
|
| 123 |
-
|
| 124 |
|
| 125 |
def process_translation(eng_script):
|
| 126 |
-
|
| 127 |
-
return urdu_script
|
| 128 |
|
|
|
|
| 129 |
def download_english(eng_script, topic):
|
| 130 |
-
|
| 131 |
-
filename = f"{topic}_Eng_{timestamp}.txt"
|
| 132 |
-
return save_file(eng_script, filename)
|
| 133 |
|
| 134 |
def download_urdu(urdu_script, topic):
|
| 135 |
-
|
| 136 |
-
filename = f"{topic}_Urdu_{timestamp}.txt"
|
| 137 |
-
return save_file(urdu_script, filename)
|
| 138 |
|
| 139 |
def download_final_urdu(edited_urdu_script, topic):
|
| 140 |
-
|
| 141 |
-
filename = f"{topic}_Urdu_Final_{timestamp}.txt"
|
| 142 |
-
return save_file(edited_urdu_script, filename)
|
| 143 |
|
| 144 |
# β
Gradio UI
|
| 145 |
with gr.Blocks() as app:
|
|
@@ -148,46 +100,36 @@ with gr.Blocks() as app:
|
|
| 148 |
topic_input = gr.Textbox(label="Enter Topic")
|
| 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 |
-
#
|
| 162 |
-
gr.Markdown("### β Edit Urdu Script Below")
|
| 163 |
-
editable_urdu = gr.Textbox(label="Edited Urdu Script", interactive=True)
|
| 164 |
-
# Save Edited Urdu Script
|
| 165 |
-
save_button = gr.Button("Save Edited Urdu Script")
|
| 166 |
-
|
| 167 |
-
# Update the editable Urdu box with translated script
|
| 168 |
translate_button.click(lambda x: x, inputs=[urdu_output], outputs=[editable_urdu])
|
| 169 |
|
| 170 |
-
|
| 171 |
download_eng_button = gr.Button("π₯ Download English Script")
|
| 172 |
download_eng_file = gr.File()
|
| 173 |
|
| 174 |
download_urdu_button = gr.Button("π₯ Download Translated Urdu Script")
|
| 175 |
download_urdu_file = gr.File()
|
| 176 |
|
| 177 |
-
# Download Edited Urdu Script
|
| 178 |
download_final_urdu_button = gr.Button("π₯ Download Edited Urdu Script")
|
| 179 |
download_final_urdu_file = gr.File()
|
| 180 |
|
| 181 |
-
# Save the edited script
|
| 182 |
save_button.click(save_edited_urdu, inputs=[editable_urdu, topic_input], outputs=[download_final_urdu_file])
|
| 183 |
-
|
| 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()
|
|
|
|
| 5 |
import torch
|
| 6 |
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
| 7 |
|
|
|
|
| 8 |
# β
Set API Key
|
| 9 |
+
os.environ["GROQ_API_KEY"] = "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" # Replace with your API key
|
|
|
|
|
|
|
| 10 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 11 |
|
|
|
|
| 12 |
# β
Load M2M-100 Model
|
| 13 |
model_name = "facebook/m2m100_418M"
|
| 14 |
tokenizer = M2M100Tokenizer.from_pretrained(model_name)
|
| 15 |
model = M2M100ForConditionalGeneration.from_pretrained(model_name)
|
| 16 |
|
| 17 |
+
# β
Function to Generate English Script
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def generate_script(topic, duration):
|
| 19 |
try:
|
| 20 |
+
words_per_minute = 130
|
| 21 |
target_words = duration * words_per_minute
|
| 22 |
|
| 23 |
response = client.chat.completions.create(
|
| 24 |
+
messages=[{"role": "user", "content": f"Generate a plain, well-structured educational script about {topic} in English with approximately {target_words} words."}],
|
|
|
|
|
|
|
| 25 |
model="llama-3.3-70b-versatile"
|
| 26 |
)
|
| 27 |
|
| 28 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
+
print(f"β Error in script generation: {str(e)}")
|
| 31 |
+
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# β
Function to Translate to Urdu
|
| 34 |
def translate_to_urdu(english_script):
|
| 35 |
try:
|
| 36 |
tokenizer.src_lang = "en"
|
| 37 |
+
max_length = 500
|
| 38 |
input_chunks = [english_script[i:i+max_length] for i in range(0, len(english_script), max_length)]
|
| 39 |
|
| 40 |
translated_chunks = []
|
|
|
|
| 47 |
forced_bos_token_id=tokenizer.get_lang_id("ur"),
|
| 48 |
num_beams=2
|
| 49 |
)
|
| 50 |
+
translated_chunks.append(tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0])
|
|
|
|
| 51 |
|
| 52 |
return " ".join(translated_chunks)
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
return f"β Error in translation: {str(e)}"
|
| 55 |
|
| 56 |
+
# β
Save Edited Urdu Script
|
|
|
|
| 57 |
def save_edited_urdu(edited_urdu_script, topic):
|
| 58 |
timestamp = datetime.datetime.now().strftime("%Y_%m_%d")
|
| 59 |
filename = f"{topic}_Urdu_Final_{timestamp}.txt"
|
| 60 |
+
filepath = os.path.join(os.getcwd(), filename)
|
| 61 |
|
| 62 |
with open(filepath, "w", encoding="utf-8") as file:
|
| 63 |
file.write(edited_urdu_script)
|
| 64 |
|
| 65 |
+
return filepath
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# β
Save File Utility
|
| 68 |
def save_file(content, filename):
|
| 69 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 70 |
+
filename = f"{filename}_{timestamp}.txt"
|
| 71 |
filepath = os.path.join(os.getcwd(), filename)
|
| 72 |
|
| 73 |
with open(filepath, "w", encoding="utf-8") as file:
|
|
|
|
| 75 |
|
| 76 |
return filepath
|
| 77 |
|
| 78 |
+
# β
Process Request
|
| 79 |
def process_request(topic, duration):
|
| 80 |
eng_script = generate_script(topic, duration)
|
| 81 |
+
return eng_script, "", "" # Clears Urdu & Edited Urdu textboxes
|
|
|
|
| 82 |
|
| 83 |
def process_translation(eng_script):
|
| 84 |
+
return translate_to_urdu(eng_script)
|
|
|
|
| 85 |
|
| 86 |
+
# β
Download Functions
|
| 87 |
def download_english(eng_script, topic):
|
| 88 |
+
return save_file(eng_script, f"{topic}_Eng")
|
|
|
|
|
|
|
| 89 |
|
| 90 |
def download_urdu(urdu_script, topic):
|
| 91 |
+
return save_file(urdu_script, f"{topic}_Urdu")
|
|
|
|
|
|
|
| 92 |
|
| 93 |
def download_final_urdu(edited_urdu_script, topic):
|
| 94 |
+
return save_file(edited_urdu_script, f"{topic}_Urdu_Final")
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# β
Gradio UI
|
| 97 |
with gr.Blocks() as app:
|
|
|
|
| 100 |
topic_input = gr.Textbox(label="Enter Topic")
|
| 101 |
duration_input = gr.Slider(minimum=1, maximum=30, step=1, label="Duration (minutes)")
|
| 102 |
generate_button = gr.Button("Generate English Script")
|
| 103 |
+
|
| 104 |
eng_output = gr.Textbox(label="Generated English Script", interactive=False)
|
| 105 |
+
urdu_output = gr.Textbox(label="Translated Urdu Script", interactive=True)
|
| 106 |
+
editable_urdu = gr.Textbox(label="Edited Urdu Script", interactive=True)
|
| 107 |
+
|
| 108 |
generate_button.click(
|
| 109 |
process_request,
|
| 110 |
inputs=[topic_input, duration_input],
|
| 111 |
outputs=[eng_output, urdu_output, editable_urdu] # Clears Urdu & Edited Urdu textboxes
|
| 112 |
)
|
| 113 |
+
|
| 114 |
translate_button = gr.Button("Generate Urdu Script")
|
|
|
|
| 115 |
translate_button.click(process_translation, inputs=[eng_output], outputs=[urdu_output])
|
| 116 |
|
| 117 |
+
# β
Update editable Urdu box after translation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
translate_button.click(lambda x: x, inputs=[urdu_output], outputs=[editable_urdu])
|
| 119 |
|
| 120 |
+
save_button = gr.Button("Save Edited Urdu Script")
|
| 121 |
download_eng_button = gr.Button("π₯ Download English Script")
|
| 122 |
download_eng_file = gr.File()
|
| 123 |
|
| 124 |
download_urdu_button = gr.Button("π₯ Download Translated Urdu Script")
|
| 125 |
download_urdu_file = gr.File()
|
| 126 |
|
|
|
|
| 127 |
download_final_urdu_button = gr.Button("π₯ Download Edited Urdu Script")
|
| 128 |
download_final_urdu_file = gr.File()
|
| 129 |
|
|
|
|
| 130 |
save_button.click(save_edited_urdu, inputs=[editable_urdu, topic_input], outputs=[download_final_urdu_file])
|
|
|
|
|
|
|
| 131 |
download_eng_button.click(download_english, inputs=[eng_output, topic_input], outputs=[download_eng_file])
|
| 132 |
download_urdu_button.click(download_urdu, inputs=[urdu_output, topic_input], outputs=[download_urdu_file])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
download_final_urdu_button.click(download_final_urdu, inputs=[editable_urdu, topic_input], outputs=[download_final_urdu_file])
|
| 134 |
|
| 135 |
app.launch()
|