Spaces:
Running
Running
import gradio as gr | |
import json | |
from predict_risks_ft import process_data | |
import ast # fallback if json.loads fails (e.g., for single quotes) | |
def predict_from_json(json_input): | |
try: | |
if not json_input or not json_input.strip(): | |
return {"error": "No input provided."} | |
try: | |
# Try to parse as JSON | |
input_data = json.loads(json_input) | |
except json.JSONDecodeError: | |
# If JSON fails, try Python literal (e.g., {'key': 'value'}) | |
input_data = ast.literal_eval(json_input) | |
print("Parsed input:", input_data) | |
results = process_data(input_data) | |
if isinstance(results, (dict, list)): | |
return results | |
else: | |
return {"error": "Invalid output format, expected dict or list."} | |
except Exception as e: | |
return {"error": f"Processing error: {str(e)}"} | |
# Create the Gradio interface | |
demo = gr.Interface( | |
fn=predict_from_json, | |
inputs=gr.Textbox( | |
placeholder="Insert JSON here...", | |
label="Patient Data (JSON)" | |
), # This allows user to input JSON data as text | |
outputs=gr.JSON(label="Prediction Result"), # Return result as JSON | |
title="Health Risk Classifier", | |
description="Insert patient medical data in JSON format to predict health risks.", | |
) | |
if __name__ == "__main__": | |
demo.launch(share=True) # Share=True to generate a public link | |