Spaces:
Sleeping
Sleeping
| import requests | |
| import time | |
| import os | |
| BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000") | |
| def test_real_data(): | |
| # Real data: A short news paragraph about Uganda | |
| real_text = ( | |
| "Uganda, famously known as the Pearl of Africa, is a landlocked country in East Africa. " | |
| "It is bordered to the east by Kenya, to the north by South Sudan, to the west by the Democratic Republic of the Congo, " | |
| "to the south-west by Rwanda, and to the south by Tanzania. The southern part of the country includes a substantial portion of Lake Victoria, " | |
| "shared with Kenya and Tanzania. Uganda takes its name from the Buganda kingdom, which encompasses a large portion of the south of the country, " | |
| "including the capital Kampala." | |
| ) | |
| target_language = "Luganda" | |
| print("Submitting real data for translation to:", target_language) | |
| # 1. Submit text | |
| resp = requests.post( | |
| f"{BACKEND_URL}/pipeline/submit", | |
| json={"text": real_text, "target_language": target_language}, | |
| timeout=15 | |
| ) | |
| resp.raise_for_status() | |
| job_id = resp.json()["request_id"] | |
| print(f"Job submitted. ID: {job_id}") | |
| # 2. Poll for completion | |
| start = time.time() | |
| while True: | |
| status_resp = requests.get(f"{BACKEND_URL}/pipeline/status/{job_id}", timeout=10) | |
| status_resp.raise_for_status() | |
| data = status_resp.json() | |
| if data["status"] == "completed": | |
| print(f"\n✅ Job completed in {time.time() - start:.2f} seconds!") | |
| result = data["result"] | |
| print("\n--- SUMMARY ---") | |
| print(result["summary"]) | |
| print("\n--- TRANSLATION (Luganda) ---") | |
| print(result["translated_summary"]) | |
| break | |
| elif data["status"] == "failed": | |
| print(f"\n❌ Job failed: {data.get('error')}") | |
| return | |
| print(".", end="", flush=True) | |
| time.sleep(2) | |
| # 3. Download Audio | |
| print(f"\nDownloading audio for job {job_id}...") | |
| audio_resp = requests.get(f"{BACKEND_URL}/pipeline/audio/{job_id}", timeout=10) | |
| if audio_resp.status_code == 200: | |
| audio_file = "real_data_audio.wav" | |
| with open(audio_file, "wb") as f: | |
| f.write(audio_resp.content) | |
| print(f"✅ Audio downloaded successfully: {audio_file} ({len(audio_resp.content)} bytes)") | |
| else: | |
| print(f"❌ Failed to download audio. Status: {audio_resp.status_code}, Detail: {audio_resp.text}") | |
| if __name__ == "__main__": | |
| test_real_data() | |