# gradio_app.py
import gradio as gr
import requests
import os

# URL of the backend API (if hosted separately, otherwise use local endpoints)
API_URL = os.getenv("API_URL", "http://localhost:8000")

def extract_interface(text):
    response = requests.post(f"{API_URL}/extract", json={"text": text})
    if response.ok:
        return response.json()["entities"]
    else:
        return {"error": response.text}

def summarize_interface(text):
    response = requests.post(f"{API_URL}/summarize", json={"text": text})
    if response.ok:
        return response.json()["summary"]
    else:
        return {"error": response.text}

with gr.Blocks(title="Materials AI Extraction Demo") as demo:
    gr.Markdown("## Materials Science AI Extraction")
    with gr.Tabs():
        with gr.TabItem("Extract Entities"):
            input_text = gr.Textbox(label="Enter Materials Science Text", lines=5)
            output_entities = gr.JSON(label="Extracted Entities")
            extract_btn = gr.Button("Extract")
            extract_btn.click(fn=extract_interface, inputs=input_text, outputs=output_entities)
        with gr.TabItem("Summarize Text"):
            summary_input = gr.Textbox(label="Enter Text to Summarize", lines=5)
            summary_output = gr.Textbox(label="Summary")
            summarize_btn = gr.Button("Summarize")
            summarize_btn.click(fn=summarize_interface, inputs=summary_input, outputs=summary_output)

demo.launch()