Update app.py
Browse files2 unmodified lines34567819 unmodified lines28293031323334353637383940414243444546474849501 unmodified line525354555657581 unmodified line6061626364656667686970717273747529 unmodified lines1051061071081091101117 unmodified lines1191201211221231241251261271281291303 unmodified lines1341351361371381391402 unmodified linesfrom datetime import datetimeimport requestsimport os# Initialize databasedef init_db():19 unmodified linesif not text.strip():return text# For demo purposes - replace with your actual translation API# This is a mock translation that shows the patterntranslations = {"Urdu": f"اردو ترجمہ: {text}","Punjabi": f"ਪੰਜਾਬੀ ਅਨੁਵਾਦ: {text}","Sindhi": f"سنڌي ترجمو: {text}","Pashto": f"پښتو ژباړه: {text}"}# In a real implementation, you would call your translation API here:# response = requests.post(# "YOUR_TRANSLATION_API_ENDPOINT",# json={"text": text, "target_language": target_language},# headers={"Authorization": f"Bearer {os.getenv('TRANSLATION_API_KEY')}"}# )# return response.json()['translated_text']return translations.get(target_language, text)# --- Submission Handler ---1 unmodified lineif not post_text.strip():return gr.Markdown("**⚠️ Please enter text to translate**"), gr.Markdown()translated = translate_text(post_text, language)# Save to databaseconn = sqlite3.connect("announcements.db")1 unmodified linecursor.execute("""INSERT INTO announcements (category, original_text, translated_text, language)VALUES (?, ?, ?, ?)""", (category, post_text, translated, language))conn.commit()conn.close()# Format the outputtimestamp = "Just Now"new_post_md = f"""## 🆕 New {category} (Posted {timestamp})**Original:** {post_text}**Translated ({language}):** {translated}---"""29 unmodified linestimestamp = datetime.strptime(post[5], "%Y-%m-%d %H:%M:%S").strftime("%b %d, %Y %I:%M %p")markdown += f"""**Original:** {post[2]}**Translated ({post[4]}):** {post[3]}*Posted on {timestamp}*---"""7 unmodified lines# Input Sectionwith gr.Row():post_text = gr.Textbox(label="Your Message", lines=4, placeholder="Type your announcement here...")with gr.Row():category = gr.Dropdown(["Announcements", "Events", "Jobs", "Lost & Found"],label="Category", value="Announcements")language = gr.Dropdown(["Urdu", "Punjabi", "Sindhi", "Pashto"],label="Translate To", value="Urdu")submit_btn = gr.Button("Post Announcement", variant="primary")3 unmodified linesall_posts = gr.Markdown(label="Community Bulletin")# Load announcements on startupdemo.load(load_announcements, outputs=all_posts)# Update both sections on submitsubmit_btn.click(2 unmodified lines345678919 unmodified lines2930313233343536373839404142431 unmodified line4546474849505152531 unmodified line555657585960616263646566676869707129 unmodified lines1011021031041051061071087 unmodified lines1161171181191201211221231241251261271281291301311321331341351361371383 unmodified lines1421431441451461471481491501512 unmodified linesfrom datetime import datetimeimport requestsimport osimport html# Initialize databasedef init_db():19 unmodified linesif not text.strip():return text# For real implementation, use your translation API here# This is a mock version that simulates translationstranslations = {"Urdu": f"(اردو ترجمہ) {text}","Punjabi": f"(ਪੰਜਾਬੀ ਅਨੁਵਾਦ) {text}","Sindhi": f"(سنڌي ترجمو) {text}","Pashto": f"(پښتو ژباړه) {text}"}return translations.get(target_language, text)# --- Submission Handler ---1 unmodified lineif not post_text.strip():return gr.Markdown("**⚠️ Please enter text to translate**"), gr.Markdown()# Escape HTML to prevent XSSsafe_text = html.escape(post_text)translated = translate_text(safe_text, language)# Save to databaseconn = sqlite3.connect("announcements.db")1 unmodified linecursor.execute("""INSERT INTO announcements (category, original_text, translated_text, language)VALUES (?, ?, ?, ?)""", (category, safe_text, translated, language))conn.commit()conn.close()# Format the output with proper line breakstimestamp = "Just Now"new_post_md = f"""## 🆕 New {category} (Posted {timestamp})**Original:** {safe_text}**Translated ({language}):**{translated}---"""29 unmodified linestimestamp = datetime.strptime(post[5], "%Y-%m-%d %H:%M:%S").strftime("%b %d, %Y %I:%M %p")markdown += f"""**Original:** {post[2]}**Translated ({post[4]}):**{post[3]}*Posted on {timestamp}*---"""7 unmodified lines# Input Sectionwith gr.Row():post_text = gr.Textbox(label="Your Message",lines=4,placeholder="Type your announcement here...",max_lines=6)with gr.Row():category = gr.Dropdown(["Announcements", "Events", "Jobs", "Lost & Found"],label="Category",value="Announcements")language = gr.Dropdown(["Urdu", "Punjabi", "Sindhi", "Pashto"],label="Translate To",value="Urdu")submit_btn = gr.Button("Post Announcement", variant="primary")3 unmodified linesall_posts = gr.Markdown(label="Community Bulletin")# Load announcements on startupdemo.load(load_announcements,outputs=all_posts)# Update both sections on submitsubmit_btn.click(
app.py+37-26