Spaces:
Runtime error
Runtime error
Commit
·
a09c5e4
1
Parent(s):
1858901
Update
Browse files- backend.py +25 -0
- gradio_frontend.py +79 -0
- img/API.png +0 -0
- requirements.txt +0 -0
backend.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
|
| 4 |
+
app = FastAPI()
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@app.get("/")
|
| 8 |
+
async def root():
|
| 9 |
+
return {"message": "Hello World"}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@app.get("/hello/{name}")
|
| 13 |
+
async def say_hello(name: str):
|
| 14 |
+
return {"message": f"Hello, {name.upper()}"}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class Item(BaseModel):
|
| 18 |
+
name: str
|
| 19 |
+
description: str | None = None
|
| 20 |
+
price: float
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@app.post("/items/")
|
| 24 |
+
async def create_item(item: Item):
|
| 25 |
+
return item
|
gradio_frontend.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# URL base da sua API FastAPI.
|
| 6 |
+
# Certifique-se de que o backend (backend.py) está rodando.
|
| 7 |
+
API_URL = "http://127.0.0.1:8000"
|
| 8 |
+
|
| 9 |
+
# --- Funções que interagem com a API ---
|
| 10 |
+
|
| 11 |
+
def get_root_message():
|
| 12 |
+
"""Chama o endpoint GET / """
|
| 13 |
+
try:
|
| 14 |
+
response = requests.get(f"{API_URL}/")
|
| 15 |
+
response.raise_for_status() # Lança um erro para respostas 4xx/5xx
|
| 16 |
+
return response.json().get("message", "Resposta inesperada da API.")
|
| 17 |
+
except requests.exceptions.RequestException as e:
|
| 18 |
+
return f"Erro ao conectar com a API: {e}"
|
| 19 |
+
|
| 20 |
+
def get_hello_message(name):
|
| 21 |
+
"""Chama o endpoint GET /hello/{name}"""
|
| 22 |
+
if not name:
|
| 23 |
+
return "Por favor, insira um nome."
|
| 24 |
+
try:
|
| 25 |
+
response = requests.get(f"{API_URL}/hello/{name}")
|
| 26 |
+
response.raise_for_status()
|
| 27 |
+
return response.json().get("message", "Resposta inesperada da API.")
|
| 28 |
+
except requests.exceptions.RequestException as e:
|
| 29 |
+
return f"Erro ao conectar com a API: {e}"
|
| 30 |
+
|
| 31 |
+
def create_item(name, description, price):
|
| 32 |
+
"""Chama o endpoint POST /items/"""
|
| 33 |
+
if not name or price is None:
|
| 34 |
+
return "Nome e Preço são campos obrigatórios."
|
| 35 |
+
|
| 36 |
+
item_data = {
|
| 37 |
+
"name": name,
|
| 38 |
+
"description": description,
|
| 39 |
+
"price": price
|
| 40 |
+
}
|
| 41 |
+
try:
|
| 42 |
+
response = requests.post(f"{API_URL}/items/", json=item_data)
|
| 43 |
+
response.raise_for_status()
|
| 44 |
+
return response.json()
|
| 45 |
+
except requests.exceptions.RequestException as e:
|
| 46 |
+
return f"Erro ao conectar com a API: {e}"
|
| 47 |
+
|
| 48 |
+
# --- Construção da Interface com Gradio ---
|
| 49 |
+
|
| 50 |
+
import gradio as gr
|
| 51 |
+
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("# Interface para API FastAPI")
|
| 54 |
+
|
| 55 |
+
with gr.Tab("Saudação (/)"):
|
| 56 |
+
btn_root = gr.Button("Chamar API Raiz (/)")
|
| 57 |
+
output_root = gr.Textbox(label="Resposta da API")
|
| 58 |
+
btn_root.click(fn=get_root_message, inputs=None, outputs=output_root)
|
| 59 |
+
|
| 60 |
+
with gr.Tab("Saudação com Nome (/hello/{name})"):
|
| 61 |
+
input_name = gr.Textbox(label="Digite seu nome")
|
| 62 |
+
btn_hello = gr.Button("Enviar")
|
| 63 |
+
output_hello = gr.Textbox(label="Resposta da API")
|
| 64 |
+
btn_hello.click(fn=get_hello_message, inputs=input_name, outputs=output_hello)
|
| 65 |
+
|
| 66 |
+
with gr.Tab("Criar Item (/items/)"):
|
| 67 |
+
input_item_name = gr.Textbox(label="Nome do Item")
|
| 68 |
+
input_item_desc = gr.Textbox(label="Descrição do Item (Opcional)")
|
| 69 |
+
input_item_price = gr.Number(label="Preço do Item")
|
| 70 |
+
btn_create = gr.Button("Criar Item")
|
| 71 |
+
output_item = gr.JSON(label="Item Criado (Resposta da API)")
|
| 72 |
+
btn_create.click(
|
| 73 |
+
fn=create_item,
|
| 74 |
+
inputs=[input_item_name, input_item_desc, input_item_price],
|
| 75 |
+
outputs=output_item
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch(share=True)
|
img/API.png
ADDED
|
requirements.txt
ADDED
|
Binary file (222 Bytes). View file
|
|
|