Madras1 commited on
Commit
8235c8e
·
verified ·
1 Parent(s): 9941488

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. README.md +36 -11
  3. app.py +79 -0
  4. requirements.txt +5 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install dependencies
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ # Copy application
10
+ COPY app.py .
11
+
12
+ # HuggingFace Spaces usa porta 7860
13
+ EXPOSE 7860
14
+
15
+ # Run the application
16
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,11 +1,36 @@
1
- ---
2
- title: Anima
3
- emoji: 🏢
4
- colorFrom: gray
5
- colorTo: blue
6
- sdk: docker
7
- pinned: false
8
- license: apache-2.0
9
- ---
10
-
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Anima API
3
+ emoji: 🎭
4
+ colorFrom: purple
5
+ colorTo: blue
6
+ sdk: docker
7
+ pinned: false
8
+ ---
9
+
10
+ # Anima API
11
+
12
+ Backend para o Anima - AI Avatar Chat.
13
+
14
+ ## Endpoints
15
+
16
+ - `GET /health` - Health check
17
+ - `POST /chat` - Enviar mensagem e receber resposta com áudio
18
+
19
+ ## Uso
20
+
21
+ ```python
22
+ import requests
23
+
24
+ response = requests.post(
25
+ "https://seu-space.hf.space/chat",
26
+ json={"message": "Olá!", "history": []}
27
+ )
28
+ data = response.json()
29
+ print(data["text"]) # Resposta do LLM
30
+ # data["audio_base64"] contém o áudio em base64
31
+ ```
32
+
33
+ ## Variáveis de Ambiente
34
+
35
+ Configure no HuggingFace Spaces Settings:
36
+ - `GROQ_API_KEY` - Sua API key do Groq
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel
4
+ import edge_tts
5
+ import asyncio
6
+ import base64
7
+ import os
8
+ from groq import Groq
9
+
10
+ app = FastAPI(title="Anima - AI Avatar Chat")
11
+
12
+ # CORS para permitir frontend
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"],
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
20
+
21
+ # Groq client
22
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
23
+
24
+ # Voz do Edge-TTS (português brasileiro feminino)
25
+ VOICE = "pt-BR-FranciscaNeural"
26
+
27
+ class ChatRequest(BaseModel):
28
+ message: str
29
+ history: list = []
30
+
31
+ class ChatResponse(BaseModel):
32
+ text: str
33
+ audio_base64: str
34
+
35
+ @app.get("/health")
36
+ async def health():
37
+ return {"status": "ok"}
38
+
39
+ @app.post("/chat", response_model=ChatResponse)
40
+ async def chat(request: ChatRequest):
41
+ try:
42
+ # Monta o histórico para o LLM
43
+ messages = [
44
+ {"role": "system", "content": "Você é Anima, uma assistente virtual amigável e prestativa. Responda de forma natural e concisa em português brasileiro."}
45
+ ]
46
+
47
+ for msg in request.history:
48
+ messages.append(msg)
49
+
50
+ messages.append({"role": "user", "content": request.message})
51
+
52
+ # Chama o Groq
53
+ completion = client.chat.completions.create(
54
+ model="llama-3.1-8b-instant",
55
+ messages=messages,
56
+ temperature=0.7,
57
+ max_tokens=500,
58
+ )
59
+
60
+ response_text = completion.choices[0].message.content
61
+
62
+ # Gera áudio com Edge-TTS
63
+ communicate = edge_tts.Communicate(response_text, VOICE)
64
+ audio_data = b""
65
+
66
+ async for chunk in communicate.stream():
67
+ if chunk["type"] == "audio":
68
+ audio_data += chunk["data"]
69
+
70
+ audio_base64 = base64.b64encode(audio_data).decode("utf-8")
71
+
72
+ return ChatResponse(text=response_text, audio_base64=audio_base64)
73
+
74
+ except Exception as e:
75
+ raise HTTPException(status_code=500, detail=str(e))
76
+
77
+ if __name__ == "__main__":
78
+ import uvicorn
79
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi==0.104.1
2
+ uvicorn==0.24.0
3
+ edge-tts==6.1.9
4
+ groq==0.4.2
5
+ python-multipart==0.0.6