Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- Dockerfile +16 -0
- app.py +47 -0
- pizzas.csv +6 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import openai
|
| 4 |
+
import os
|
| 5 |
+
import csv
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Configuração de CORS
|
| 10 |
+
origins = ["*"]
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=origins,
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Definir API key da OpenAI via variável de ambiente
|
| 20 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 21 |
+
|
| 22 |
+
# Carregar informações das pizzas do arquivo CSV
|
| 23 |
+
pizzas = []
|
| 24 |
+
with open('pizzas.csv', mode='r', encoding='utf-8') as csvfile:
|
| 25 |
+
reader = csv.DictReader(csvfile)
|
| 26 |
+
for row in reader:
|
| 27 |
+
pizzas.append(row)
|
| 28 |
+
|
| 29 |
+
@app.post("/chatbot")
|
| 30 |
+
async def chatbot(request: Request):
|
| 31 |
+
body = await request.json()
|
| 32 |
+
user_message = body.get("message")
|
| 33 |
+
response_text = ""
|
| 34 |
+
|
| 35 |
+
if "menu" in user_message.lower():
|
| 36 |
+
response_text = "Temos as seguintes pizzas:\n"
|
| 37 |
+
for pizza in pizzas:
|
| 38 |
+
response_text += f"{pizza['nome']}: {pizza['ingredientes']} - R${pizza['valor']}\n"
|
| 39 |
+
else:
|
| 40 |
+
response = openai.Completion.create(
|
| 41 |
+
model="gpt-4",
|
| 42 |
+
prompt=user_message,
|
| 43 |
+
max_tokens=150
|
| 44 |
+
)
|
| 45 |
+
response_text = response.choices[0].text.strip()
|
| 46 |
+
|
| 47 |
+
return {"response": response_text}
|
pizzas.csv
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
nome,ingredientes,valor
|
| 2 |
+
Margherita,Molho de tomate, mussarela, manjericão,25.90
|
| 3 |
+
Calabresa,Molho de tomate, mussarela, calabresa, cebola,28.90
|
| 4 |
+
Quatro Queijos,Molho de tomate, mussarela, gorgonzola, provolone, parmesão,32.50
|
| 5 |
+
Frango com Catupiry,Molho de tomate, frango desfiado, catupiry,29.90
|
| 6 |
+
Pepperoni,Molho de tomate, mussarela, pepperoni,30.00
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
python-dotenv
|
| 4 |
+
openai
|