Spaces:
Runtime error
Runtime error
Commit
·
e540e5d
1
Parent(s):
c62533b
Update
Browse files- app.py +94 -48
- requirements.txt +0 -0
app.py
CHANGED
|
@@ -1,79 +1,125 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def get_root_message():
|
| 12 |
-
"""Chama
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return f"Erro ao conectar com a API: {e}"
|
| 19 |
|
| 20 |
def get_hello_message(name):
|
| 21 |
-
"""Chama
|
| 22 |
if not name:
|
| 23 |
return "Por favor, insira um nome."
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return f"Erro ao conectar com a API: {e}"
|
| 30 |
|
| 31 |
def create_item(name, description, price):
|
| 32 |
-
"""Chama
|
| 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 |
-
|
| 43 |
-
|
| 44 |
-
return
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
# ---
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
with gr.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
btn_root.click(fn=get_root_message, inputs=None, outputs=output_root)
|
| 59 |
|
| 60 |
-
with gr.Tab("Saudação com Nome
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
btn_hello.click(fn=get_hello_message, inputs=input_name, outputs=output_hello)
|
| 65 |
|
| 66 |
-
with gr.Tab("Criar Item
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import uvicorn
|
| 5 |
import json
|
| 6 |
|
| 7 |
+
# --- Backend FastAPI (integrado no mesmo arquivo) ---
|
| 8 |
+
class Item(BaseModel):
|
| 9 |
+
name: str
|
| 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 a função do backend diretamente"""
|
| 27 |
try:
|
| 28 |
+
result = root_message()
|
| 29 |
+
return result.get("message", "Resposta inesperada da API.")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Erro: {e}"
|
|
|
|
| 32 |
|
| 33 |
def get_hello_message(name):
|
| 34 |
+
"""Chama a função do backend diretamente"""
|
| 35 |
if not name:
|
| 36 |
return "Por favor, insira um nome."
|
| 37 |
try:
|
| 38 |
+
result = hello_message(name)
|
| 39 |
+
return result.get("message", "Resposta inesperada da API.")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Erro: {e}"
|
|
|
|
| 42 |
|
| 43 |
def create_item(name, description, price):
|
| 44 |
+
"""Chama a função do backend diretamente"""
|
| 45 |
if not name or price is None:
|
| 46 |
return "Nome e Preço são campos obrigatórios."
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
+
result = create_item_backend(name, description, price)
|
| 50 |
+
# Converte o objeto Item para dict
|
| 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:
|
| 61 |
+
gr.Markdown("# 🚀 FastAPI + Gradio Integrado")
|
| 62 |
+
gr.Markdown("**Backend e Frontend em um único app!**")
|
| 63 |
|
| 64 |
+
with gr.Tab("🏠 Saudação Raiz"):
|
| 65 |
+
gr.Markdown("### Endpoint: GET /")
|
| 66 |
+
btn_root = gr.Button("Chamar API Raiz", variant="primary")
|
| 67 |
+
output_root = gr.Textbox(
|
| 68 |
+
label="Resposta da API",
|
| 69 |
+
placeholder="Clique no botão para ver a mensagem...",
|
| 70 |
+
lines=3
|
| 71 |
+
)
|
| 72 |
btn_root.click(fn=get_root_message, inputs=None, outputs=output_root)
|
| 73 |
|
| 74 |
+
with gr.Tab("👋 Saudação com Nome"):
|
| 75 |
+
gr.Markdown("### Endpoint: GET /hello/{name}")
|
| 76 |
+
with gr.Row():
|
| 77 |
+
with gr.Column():
|
| 78 |
+
input_name = gr.Textbox(
|
| 79 |
+
label="Digite seu nome",
|
| 80 |
+
placeholder="Seu nome aqui..."
|
| 81 |
+
)
|
| 82 |
+
btn_hello = gr.Button("Enviar Saudação", variant="primary")
|
| 83 |
+
with gr.Column():
|
| 84 |
+
output_hello = gr.Textbox(
|
| 85 |
+
label="Resposta da API",
|
| 86 |
+
lines=3
|
| 87 |
+
)
|
| 88 |
btn_hello.click(fn=get_hello_message, inputs=input_name, outputs=output_hello)
|
| 89 |
|
| 90 |
+
with gr.Tab("📦 Criar Item"):
|
| 91 |
+
gr.Markdown("### Endpoint: POST /items/")
|
| 92 |
+
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
+
input_item_name = gr.Textbox(
|
| 95 |
+
label="Nome do Item*",
|
| 96 |
+
placeholder="Nome do item..."
|
| 97 |
+
)
|
| 98 |
+
input_item_desc = gr.Textbox(
|
| 99 |
+
label="Descrição do Item (Opcional)",
|
| 100 |
+
placeholder="Descrição do item...",
|
| 101 |
+
lines=2
|
| 102 |
+
)
|
| 103 |
+
input_item_price = gr.Number(
|
| 104 |
+
label="Preço do Item*",
|
| 105 |
+
value=0.0
|
| 106 |
+
)
|
| 107 |
+
btn_create = gr.Button("Criar Item", variant="primary")
|
| 108 |
+
with gr.Column():
|
| 109 |
+
output_item = gr.JSON(
|
| 110 |
+
label="Item Criado (Resposta da API)"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
btn_create.click(
|
| 114 |
fn=create_item,
|
| 115 |
inputs=[input_item_name, input_item_desc, input_item_price],
|
| 116 |
outputs=output_item
|
| 117 |
)
|
| 118 |
|
| 119 |
+
# Rodapé
|
| 120 |
+
gr.Markdown("---")
|
| 121 |
+
gr.Markdown("### 🔧 Desenvolvido com Gradio + FastAPI (Integrado)")
|
| 122 |
+
|
| 123 |
+
# --- Para funcionar no Hugging Face Spaces ---
|
| 124 |
if __name__ == "__main__":
|
| 125 |
+
demo.launch()
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|