Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,16 +7,18 @@ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
import yake
|
| 9 |
from datetime import datetime
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# β
Set API Key
|
| 12 |
# os.environ["GROQ_API_KEY"] = "your_api_key_here"
|
| 13 |
# client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 14 |
|
| 15 |
# β
Set API Key
|
| 16 |
-
os.environ["GROQ_API_KEY"] = "
|
| 17 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 18 |
|
| 19 |
-
# β
Load M2M-100 Model
|
| 20 |
# model_name = "facebook/m2m100_418M"
|
| 21 |
# tokenizer = M2M100Tokenizer.from_pretrained(model_name)
|
| 22 |
# model = M2M100ForConditionalGeneration.from_pretrained(model_name)
|
|
@@ -43,15 +45,6 @@ def generate_script(topic, duration):
|
|
| 43 |
except Exception as e:
|
| 44 |
return f"β Error in script generation: {str(e)}"
|
| 45 |
|
| 46 |
-
# # β
Function to Extract Keywords
|
| 47 |
-
# def extract_keywords(script):
|
| 48 |
-
# words = word_tokenize(script.lower())
|
| 49 |
-
# stop_words = set(stopwords.words('english'))
|
| 50 |
-
# filtered_words = [word for word in words if word.isalnum() and word not in stop_words]
|
| 51 |
-
# word_freq = Counter(filtered_words)
|
| 52 |
-
# keywords = [word for word, freq in word_freq.most_common(10)]
|
| 53 |
-
# return ", ".join(keywords)
|
| 54 |
-
|
| 55 |
|
| 56 |
# β
Function to Extract Keywords Using YAKE
|
| 57 |
def extract_keywords(script):
|
|
@@ -69,22 +62,6 @@ def extract_keywords(script):
|
|
| 69 |
return f"β Error extracting keywords: {str(e)}"
|
| 70 |
|
| 71 |
|
| 72 |
-
# # β
Function to Save Edited or Generated Keywords
|
| 73 |
-
# def save_keywords_file(keywords):
|
| 74 |
-
# filename = "keywords.txt"
|
| 75 |
-
# with open(filename, "w", encoding="utf-8") as f:
|
| 76 |
-
# f.write(keywords)
|
| 77 |
-
# return filename
|
| 78 |
-
|
| 79 |
-
# def save_keywords_file(keywords):
|
| 80 |
-
# if not keywords.strip():
|
| 81 |
-
# return "β No keywords available to save."
|
| 82 |
-
|
| 83 |
-
# filename = "keywords.txt"
|
| 84 |
-
# with open(filename, "w", encoding="utf-8") as f:
|
| 85 |
-
# f.write(keywords)
|
| 86 |
-
# return filename
|
| 87 |
-
|
| 88 |
def save_keywords_file(keywords, topic):
|
| 89 |
today = datetime.today().strftime('%Y_%b_%d')
|
| 90 |
filename = f"Keywords/{topic}_Keyword_{today}.txt"
|
|
@@ -120,17 +97,20 @@ def save_keywords_file(keywords, topic):
|
|
| 120 |
def translate_to_urdu(english_script):
|
| 121 |
try:
|
| 122 |
# β
Define a max chunk size (Google Translator has a limit)
|
| 123 |
-
max_chunk_size = 5000
|
| 124 |
chunks = [english_script[i:i + max_chunk_size] for i in range(0, len(english_script), max_chunk_size)]
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
|
| 129 |
return " ".join(translated_chunks) # β
Join all translated chunks
|
| 130 |
except Exception as e:
|
| 131 |
return f"β Error in translation: {str(e)}"
|
| 132 |
|
| 133 |
|
|
|
|
| 134 |
def save_english_file(content, topic):
|
| 135 |
today = datetime.today().strftime('%Y_%b_%d') # Format: 2025_Feb_21
|
| 136 |
filename = f"English_Scripts/{topic}_Eng_{today}.txt"
|
|
@@ -162,6 +142,22 @@ def finalize_process():
|
|
| 162 |
return "β
Script Generation Completed Successfully!"
|
| 163 |
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
|
| 167 |
# β
Gradio UI
|
|
@@ -182,7 +178,7 @@ with gr.Blocks() as app:
|
|
| 182 |
extract_keywords_btn = gr.Button("π Extract Keywords")
|
| 183 |
keyword_output = gr.Textbox(label="π Extracted Keywords", interactive=True)
|
| 184 |
download_keywords_btn = gr.Button("β¬οΈ Download Keywords")
|
| 185 |
-
download_keywords_btn.click(save_keywords_file, inputs=[keyword_output], outputs=[gr.File()])
|
| 186 |
|
| 187 |
translate_button = gr.Button("Generate Urdu Script")
|
| 188 |
urdu_output = gr.Textbox(label="Translated Urdu Script", interactive=False, rtl=True)
|
|
@@ -196,7 +192,8 @@ with gr.Blocks() as app:
|
|
| 196 |
|
| 197 |
|
| 198 |
# β
Button Actions
|
| 199 |
-
generate_button.click(generate_script, inputs=[topic_input, duration_input], outputs=[eng_output])
|
|
|
|
| 200 |
extract_keywords_btn.click(extract_keywords, inputs=[eng_output], outputs=[keyword_output])
|
| 201 |
translate_button.click(translate_to_urdu, inputs=[eng_output], outputs=[urdu_output])
|
| 202 |
|
|
@@ -204,5 +201,12 @@ with gr.Blocks() as app:
|
|
| 204 |
finalize_button = gr.Button("Finalize Process")
|
| 205 |
finalize_button.click(finalize_process, outputs=[status_output])
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
app.launch()
|
|
|
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
import yake
|
| 9 |
from datetime import datetime
|
| 10 |
+
import shutil
|
| 11 |
+
import glob
|
| 12 |
|
| 13 |
# β
Set API Key
|
| 14 |
# os.environ["GROQ_API_KEY"] = "your_api_key_here"
|
| 15 |
# client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 16 |
|
| 17 |
# β
Set API Key
|
| 18 |
+
os.environ["GROQ_API_KEY"] = "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" # Replace with your API key
|
| 19 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 20 |
|
| 21 |
+
# # β
Load M2M-100 Model
|
| 22 |
# model_name = "facebook/m2m100_418M"
|
| 23 |
# tokenizer = M2M100Tokenizer.from_pretrained(model_name)
|
| 24 |
# model = M2M100ForConditionalGeneration.from_pretrained(model_name)
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"β Error in script generation: {str(e)}"
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# β
Function to Extract Keywords Using YAKE
|
| 50 |
def extract_keywords(script):
|
|
|
|
| 62 |
return f"β Error extracting keywords: {str(e)}"
|
| 63 |
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def save_keywords_file(keywords, topic):
|
| 66 |
today = datetime.today().strftime('%Y_%b_%d')
|
| 67 |
filename = f"Keywords/{topic}_Keyword_{today}.txt"
|
|
|
|
| 97 |
def translate_to_urdu(english_script):
|
| 98 |
try:
|
| 99 |
# β
Define a max chunk size (Google Translator has a limit)
|
| 100 |
+
max_chunk_size = 4500 # Stay below 5000 to be safe
|
| 101 |
chunks = [english_script[i:i + max_chunk_size] for i in range(0, len(english_script), max_chunk_size)]
|
| 102 |
|
| 103 |
+
translated_chunks = []
|
| 104 |
+
for chunk in chunks:
|
| 105 |
+
translated_chunk = GoogleTranslator(source='en', target='ur').translate(chunk)
|
| 106 |
+
translated_chunks.append(translated_chunk)
|
| 107 |
|
| 108 |
return " ".join(translated_chunks) # β
Join all translated chunks
|
| 109 |
except Exception as e:
|
| 110 |
return f"β Error in translation: {str(e)}"
|
| 111 |
|
| 112 |
|
| 113 |
+
|
| 114 |
def save_english_file(content, topic):
|
| 115 |
today = datetime.today().strftime('%Y_%b_%d') # Format: 2025_Feb_21
|
| 116 |
filename = f"English_Scripts/{topic}_Eng_{today}.txt"
|
|
|
|
| 142 |
return "β
Script Generation Completed Successfully!"
|
| 143 |
|
| 144 |
|
| 145 |
+
def clear_old_files():
|
| 146 |
+
# β
Define all directories where files are stored
|
| 147 |
+
directories = ["English_Scripts", "Urdu_Scripts", "Urdu_Final", "Keywords"]
|
| 148 |
+
|
| 149 |
+
for directory in directories:
|
| 150 |
+
if os.path.exists(directory): # β
Check if directory exists
|
| 151 |
+
files = glob.glob(f"{directory}/*") # β
Get all files in the directory
|
| 152 |
+
for file in files:
|
| 153 |
+
try:
|
| 154 |
+
os.remove(file) # β
Delete each file
|
| 155 |
+
except Exception as e:
|
| 156 |
+
print(f"β Error deleting {file}: {e}")
|
| 157 |
+
|
| 158 |
+
return "", "", "", "", "" # β
Clear all textboxes in UI
|
| 159 |
+
|
| 160 |
+
|
| 161 |
|
| 162 |
|
| 163 |
# β
Gradio UI
|
|
|
|
| 178 |
extract_keywords_btn = gr.Button("π Extract Keywords")
|
| 179 |
keyword_output = gr.Textbox(label="π Extracted Keywords", interactive=True)
|
| 180 |
download_keywords_btn = gr.Button("β¬οΈ Download Keywords")
|
| 181 |
+
download_keywords_btn.click(save_keywords_file, inputs=[keyword_output, topic_input], outputs=[gr.File()])
|
| 182 |
|
| 183 |
translate_button = gr.Button("Generate Urdu Script")
|
| 184 |
urdu_output = gr.Textbox(label="Translated Urdu Script", interactive=False, rtl=True)
|
|
|
|
| 192 |
|
| 193 |
|
| 194 |
# β
Button Actions
|
| 195 |
+
# generate_button.click(generate_script, inputs=[topic_input, duration_input], outputs=[eng_output])
|
| 196 |
+
generate_button.click(generate_script, inputs=[topic_input, duration_input], outputs=[eng_output])
|
| 197 |
extract_keywords_btn.click(extract_keywords, inputs=[eng_output], outputs=[keyword_output])
|
| 198 |
translate_button.click(translate_to_urdu, inputs=[eng_output], outputs=[urdu_output])
|
| 199 |
|
|
|
|
| 201 |
finalize_button = gr.Button("Finalize Process")
|
| 202 |
finalize_button.click(finalize_process, outputs=[status_output])
|
| 203 |
|
| 204 |
+
generate_button.click(
|
| 205 |
+
lambda topic, duration: (*clear_old_files(), generate_script(topic, duration)),
|
| 206 |
+
inputs=[topic_input, duration_input],
|
| 207 |
+
outputs=[keyword_output, urdu_output, final_edited_urdu_output, status_output] )
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
|
| 211 |
|
| 212 |
app.launch()
|