Spaces:
Runtime error
Runtime error
Commit
·
afb4dff
1
Parent(s):
fccd3fc
Back e Front
Browse files- app.py +26 -38
- backend.py +11 -5
- run.sh +0 -0
app.py
CHANGED
|
@@ -1,60 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
import uvicorn
|
| 5 |
-
import json
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
description: str | None = None
|
| 11 |
-
price: float
|
| 12 |
-
|
| 13 |
-
# Criamos as funções do backend como funções Python normais
|
| 14 |
-
def root_message():
|
| 15 |
-
return {"message": "Hello World"}
|
| 16 |
-
|
| 17 |
-
def hello_message(name: str):
|
| 18 |
-
return {"message": f"Hello, {name.upper()}"}
|
| 19 |
-
|
| 20 |
-
def create_item_backend(name: str, description: str | None = None, price: float = 0.0):
|
| 21 |
-
item = Item(name=name, description=description, price=price)
|
| 22 |
-
return item
|
| 23 |
|
| 24 |
# --- Frontend Gradio ---
|
| 25 |
def get_root_message():
|
| 26 |
-
"""Chama
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
-
return f"Erro: {e}"
|
| 32 |
|
| 33 |
def get_hello_message(name):
|
| 34 |
-
"""Chama
|
| 35 |
if not name:
|
| 36 |
return "Por favor, insira um nome."
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
return f"Erro: {e}"
|
| 42 |
|
| 43 |
def create_item(name, description, price):
|
| 44 |
-
"""Chama
|
| 45 |
if not name or price is None:
|
| 46 |
return "Nome e Preço são campos obrigatórios."
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
return
|
| 52 |
-
"name": result.name,
|
| 53 |
-
"description": result.description,
|
| 54 |
-
"price": result.price
|
| 55 |
-
}
|
| 56 |
except Exception as e:
|
| 57 |
-
return f"Erro: {e}"
|
| 58 |
|
| 59 |
# --- Interface Gradio ---
|
| 60 |
with gr.Blocks(title="FastAPI + Gradio Integrado") as demo:
|
|
@@ -118,7 +106,7 @@ with gr.Blocks(title="FastAPI + Gradio Integrado") as demo:
|
|
| 118 |
|
| 119 |
# Rodapé
|
| 120 |
gr.Markdown("---")
|
| 121 |
-
gr.Markdown("### 🔧 Desenvolvido com Gradio + FastAPI (
|
| 122 |
|
| 123 |
# --- Para funcionar no Hugging Face Spaces ---
|
| 124 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# URL do nosso backend FastAPI.
|
| 5 |
+
# Usamos 127.0.0.1 (localhost) porque ambos os processos rodarão na mesma máquina/container.
|
| 6 |
+
BACKEND_URL = "http://127.0.0.1:8000"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# --- Frontend Gradio ---
|
| 9 |
def get_root_message():
|
| 10 |
+
"""Chama o endpoint GET / via HTTP"""
|
| 11 |
try:
|
| 12 |
+
response = requests.get(f"{BACKEND_URL}/")
|
| 13 |
+
response.raise_for_status() # Lança um erro para códigos de status 4xx/5xx
|
| 14 |
+
return response.json().get("message", "Resposta inesperada da API.")
|
| 15 |
except Exception as e:
|
| 16 |
+
return f"Erro ao chamar a API: {e}"
|
| 17 |
|
| 18 |
def get_hello_message(name):
|
| 19 |
+
"""Chama o endpoint GET /hello/{name} via HTTP"""
|
| 20 |
if not name:
|
| 21 |
return "Por favor, insira um nome."
|
| 22 |
try:
|
| 23 |
+
response = requests.get(f"{BACKEND_URL}/hello/{name}")
|
| 24 |
+
response.raise_for_status()
|
| 25 |
+
return response.json().get("message", "Resposta inesperada da API.")
|
| 26 |
except Exception as e:
|
| 27 |
+
return f"Erro ao chamar a API: {e}"
|
| 28 |
|
| 29 |
def create_item(name, description, price):
|
| 30 |
+
"""Chama o endpoint POST /items/ via HTTP"""
|
| 31 |
if not name or price is None:
|
| 32 |
return "Nome e Preço são campos obrigatórios."
|
| 33 |
|
| 34 |
+
item_data = {
|
| 35 |
+
"name": name,
|
| 36 |
+
"description": description,
|
| 37 |
+
"price": price
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
try:
|
| 41 |
+
response = requests.post(f"{BACKEND_URL}/items/", json=item_data)
|
| 42 |
+
response.raise_for_status()
|
| 43 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
+
return f"Erro ao chamar a API: {e}"
|
| 46 |
|
| 47 |
# --- Interface Gradio ---
|
| 48 |
with gr.Blocks(title="FastAPI + Gradio Integrado") as demo:
|
|
|
|
| 106 |
|
| 107 |
# Rodapé
|
| 108 |
gr.Markdown("---")
|
| 109 |
+
gr.Markdown("### 🔧 Desenvolvido com Gradio (Frontend) + FastAPI (Backend)")
|
| 110 |
|
| 111 |
# --- Para funcionar no Hugging Face Spaces ---
|
| 112 |
if __name__ == "__main__":
|
backend.py
CHANGED
|
@@ -1,16 +1,20 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
|
| 4 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
@app.get("/")
|
| 8 |
-
|
| 9 |
return {"message": "Hello World"}
|
| 10 |
|
| 11 |
|
| 12 |
@app.get("/hello/{name}")
|
| 13 |
-
|
| 14 |
return {"message": f"Hello, {name.upper()}"}
|
| 15 |
|
| 16 |
|
|
@@ -20,6 +24,8 @@ class Item(BaseModel):
|
|
| 20 |
price: float
|
| 21 |
|
| 22 |
|
| 23 |
-
@app.post("/items/")
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
return item
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
|
| 4 |
+
app = FastAPI(
|
| 5 |
+
title="API de Exemplo",
|
| 6 |
+
description="Um backend simples para a interface Gradio.",
|
| 7 |
+
version="1.0.0"
|
| 8 |
+
)
|
| 9 |
|
| 10 |
|
| 11 |
@app.get("/")
|
| 12 |
+
def root():
|
| 13 |
return {"message": "Hello World"}
|
| 14 |
|
| 15 |
|
| 16 |
@app.get("/hello/{name}")
|
| 17 |
+
def say_hello(name: str):
|
| 18 |
return {"message": f"Hello, {name.upper()}"}
|
| 19 |
|
| 20 |
|
|
|
|
| 24 |
price: float
|
| 25 |
|
| 26 |
|
| 27 |
+
@app.post("/items/", response_model=Item)
|
| 28 |
+
def create_item(item: Item):
|
| 29 |
+
# Em uma aplicação real, você salvaria o item em um banco de dados.
|
| 30 |
+
# Aqui, apenas retornamos o item recebido para confirmar.
|
| 31 |
return item
|
run.sh
ADDED
|
File without changes
|