| import requests |
| import urllib.parse |
| import pandas as pd |
|
|
| |
| base_url = "https://predictionsentiments-azepf7eme8dvftaa.francecentral-01.azurewebsites.net/feeling_predictions/" |
| |
| |
|
|
| |
| sentences = [ |
| "I just got a promotion at work and I am thrilled!", |
| "My dog passed away yesterday and my heart is broken.", |
| "The weather is absolutely beautiful today, I love it.", |
| "I failed my math exam despite studying all night.", |
| "We had a wonderful family dinner with lots of laughs.", |
| "I feel so lonely and isolated in this new city.", |
| "Winning the championship was the best moment of my life.", |
| "My flight got canceled and I missed my best friend's wedding.", |
| "I am enjoying a nice, relaxing cup of coffee by the window.", |
| "Everything is going wrong today and I just want to cry." |
| ] |
|
|
| |
| all_results = [] |
|
|
| headers = {"accept": "application/json"} |
|
|
| print("Starting predictions...\n") |
|
|
| |
| for text in sentences: |
| |
| encoded_text = urllib.parse.quote(text) |
| full_url = f"{base_url}{encoded_text}" |
| |
| try: |
| |
| response = requests.get(full_url, headers=headers) |
| response.raise_for_status() |
| |
| |
| api_data = response.json() |
| |
| |
| prediction = api_data.get("feeling_result") |
| |
| |
| all_results.append({ |
| "sentence": text, |
| "prediction": prediction, |
| "status": "success" |
| }) |
| |
| print(f"Processed: '{text[:30]}...' -> {prediction.upper()}") |
| |
| except requests.exceptions.RequestException as e: |
| print(f"Error processing '{text[:30]}...': {e}") |
| |
| |
| all_results.append({ |
| "sentence": text, |
| "prediction": "ERROR", |
| "status": str(e) |
| }) |
|
|
| print("\n--- All 10 requests completed! ---") |
|
|
| |
| |
| df_results = pd.DataFrame(all_results) |
|
|
| print("\nHere is your final data table:") |
| print(df_results) |
|
|
| |
| csv_filename = "api_predictions_results.csv" |
| df_results.to_csv(csv_filename, index=False, encoding="utf-8") |
| print(f"\nResults have been successfully saved to '{csv_filename}'.") |