Spaces:
Runtime error
Runtime error
Teste 1
Browse files- Dockerfile +10 -0
- app.py +13 -0
- models.py +38 -0
- requirements.txt +4 -0
- schemas.py +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from models import analise_sentimento
|
| 3 |
+
from schemas import SentimentRequest, SentimentResponse
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
@app.post("/analisar", response_model=SentimentResponse)
|
| 8 |
+
async def analisar_sentimentos(request: SentimentRequest):
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
"""
|
| 12 |
+
resposta_completa = analise_sentimento(request.text)
|
| 13 |
+
return SentimentResponse(sentiment=resposta_completa)
|
models.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
# Carregamento do modelo
|
| 5 |
+
def carregar_modelo():
|
| 6 |
+
tokenizer = LlamaTokenizer.from_pretrained("lzw1008/Emollama-7b")
|
| 7 |
+
model = LlamaForCausalLM.from_pretrained("lzw1008/Emollama-7b", device_map="auto")
|
| 8 |
+
return tokenizer, model
|
| 9 |
+
|
| 10 |
+
tokenizer, model = carregar_modelo()
|
| 11 |
+
|
| 12 |
+
def analise_sentimento(texto: str) -> str:
|
| 13 |
+
prompt = f"""Humano:
|
| 14 |
+
Tarefa: Categorize a emoção expressada no texto como 'neutro ou sem emoção' ou identifique a presença de uma ou mais emoções das listadas (raiva, confusão, ansiedade, desgosto, medo, alegria, amor, otimismo, pessimismo, tristeza, surpresa, confiança).
|
| 15 |
+
Texto: {texto}
|
| 16 |
+
Esse texto contém a emoção:
|
| 17 |
+
|
| 18 |
+
Assistente:"""
|
| 19 |
+
|
| 20 |
+
# Tokenização com padding e attention_mask
|
| 21 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True).to(model.device)
|
| 22 |
+
|
| 23 |
+
# Geração com attention_mask explícita
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
generate_ids = model.generate(
|
| 26 |
+
inputs["input_ids"],
|
| 27 |
+
attention_mask=inputs["attention_mask"],
|
| 28 |
+
max_length=256,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
top_p=0.95,
|
| 31 |
+
temperature=0.7,
|
| 32 |
+
pad_token_id=tokenizer.eos_token_id
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Decodificação completa
|
| 36 |
+
resposta = tokenizer.decode(generate_ids[0], skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
+
return resposta
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
schemas.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
+
|
| 3 |
+
class SentimentRequest(BaseModel):
|
| 4 |
+
text: str = Field(..., min_length=1, description="Texto para análise de sentimento")
|
| 5 |
+
|
| 6 |
+
class SentimentResponse(BaseModel):
|
| 7 |
+
sentiment: str
|