Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import json | |
| import pandas as pd | |
| from flask import Flask, send_file | |
| app = Flask(__name__) | |
| def process_json(json_file): | |
| try: | |
| # Read JSON file with utf-8 encoding | |
| with open(json_file.name, 'r', encoding='utf-8') as file: | |
| data = json.load(file) | |
| # Initialize lists to store user information | |
| username_list = [] | |
| full_name_list = [] | |
| is_private_list = [] | |
| is_verified_list = [] | |
| profile_pic_url_list = [] | |
| # Loop through entries | |
| for entry in data['log']['entries']: | |
| response = entry.get('response', {}) | |
| content = response.get('content', {}) | |
| # Check if "users\"" is present in the response text | |
| response_text = content.get('text', '') | |
| if "users\"" in response_text: | |
| try: | |
| json_data = json.loads(response_text) | |
| # Loop through users and extract information | |
| for user_data in json_data.get('users', []): | |
| username_list.append(user_data.get('username', '')) | |
| full_name_list.append(user_data.get('full_name', '')) | |
| is_private_list.append(user_data.get('is_private', '')) | |
| is_verified_list.append(user_data.get('is_verified', '')) | |
| profile_pic_url_list.append(user_data.get('profile_pic_url', '')) | |
| except json.JSONDecodeError as e: | |
| return {"error": f"Error decoding JSON in entry: {e}"} | |
| # Create a DataFrame | |
| df = pd.DataFrame({ | |
| 'Username': username_list, | |
| 'Full Name': full_name_list, | |
| 'Is Private': is_private_list, | |
| 'Is Verified': is_verified_list, | |
| 'Profile Pic URL': profile_pic_url_list | |
| }) | |
| # Save the DataFrame to an Excel file | |
| excel_filename = 'output_data.xlsx' | |
| df.to_excel(excel_filename, index=False) | |
| return {"success": f"Data processed and saved to {excel_filename}", "file": excel_filename} | |
| except Exception as e: | |
| return {"error": f"An error occurred: {e}"} | |
| def download_file(filename): | |
| return send_file(filename, as_attachment=True) | |
| iface = gr.Interface(fn=process_json, inputs="file", outputs=["text", "text"]) | |
| iface.launch(share=True, app=app) | |