Spaces:
Sleeping
Sleeping
Deploy CGP Bot API with Gemini fallback
Browse files- Dockerfile +33 -0
- app.py +18 -0
- cgpcorp_api_chat.py +45 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a Python base image
|
| 2 |
+
FROM python:3.9
|
| 3 |
+
|
| 4 |
+
# Create a non-root user for security best practices
|
| 5 |
+
RUN useradd -m -u 1000 user
|
| 6 |
+
USER user
|
| 7 |
+
|
| 8 |
+
# Set environment variable for user's local bin path
|
| 9 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 10 |
+
|
| 11 |
+
# Set environment variables to prevent Python from buffering stdout/stderr
|
| 12 |
+
ENV PYTHONUNBUFFERED 1
|
| 13 |
+
|
| 14 |
+
# IMPORTANT FIX: Set Hugging Face cache directory to a writable location
|
| 15 |
+
ENV HF_HOME=/app/hf_cache
|
| 16 |
+
|
| 17 |
+
# Set the working directory inside the container
|
| 18 |
+
WORKDIR /app
|
| 19 |
+
|
| 20 |
+
# Copy requirements.txt and install Python dependencies
|
| 21 |
+
COPY --chown=user requirements.txt .
|
| 22 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 23 |
+
pip install --no-cache-dir -r requirements.txt
|
| 24 |
+
|
| 25 |
+
# Copy the application file
|
| 26 |
+
# The app.py will download models from Hugging Face Hub, so no need to copy model dirs here.
|
| 27 |
+
COPY --chown=user ./app.py /app/app.py
|
| 28 |
+
COPY --chown=user ./cgpcorp_api_chat.py /app/cgpcorp_api_chat.py
|
| 29 |
+
COPY --chown=user ./README.md /app/README.md
|
| 30 |
+
|
| 31 |
+
# Command to run the application using uvicorn
|
| 32 |
+
EXPOSE 7860
|
| 33 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from cgpcorp_api_chat import translate_text
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="CGP Corp Chatbot API")
|
| 6 |
+
|
| 7 |
+
class TranslationRequest(BaseModel):
|
| 8 |
+
text: str
|
| 9 |
+
direction: str # "en-fr" or "fr-en"
|
| 10 |
+
|
| 11 |
+
@app.get("/")
|
| 12 |
+
def home():
|
| 13 |
+
return {"message": "✅ CGP Corp Bot API running on Hugging Face Spaces"}
|
| 14 |
+
|
| 15 |
+
@app.post("/translate")
|
| 16 |
+
def translate(req: TranslationRequest):
|
| 17 |
+
result = translate_text(req.text, req.direction)
|
| 18 |
+
return {"translated_text": result}
|
cgpcorp_api_chat.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 4 |
+
|
| 5 |
+
# Load Hugging Face models
|
| 6 |
+
MODEL_EN_FR = "cgpcorpbot/cgp_model_en-fr"
|
| 7 |
+
MODEL_FR_EN = "cgpcorpbot/cgp_model_fr-en"
|
| 8 |
+
|
| 9 |
+
print("Loading EN->FR model...")
|
| 10 |
+
tokenizer_en_fr = AutoTokenizer.from_pretrained(MODEL_EN_FR)
|
| 11 |
+
model_en_fr = AutoModelForSeq2SeqLM.from_pretrained(MODEL_EN_FR)
|
| 12 |
+
translator_en_fr = pipeline("translation", model=model_en_fr, tokenizer=tokenizer_en_fr)
|
| 13 |
+
|
| 14 |
+
print("Loading FR->EN model...")
|
| 15 |
+
tokenizer_fr_en = AutoTokenizer.from_pretrained(MODEL_FR_EN)
|
| 16 |
+
model_fr_en = AutoModelForSeq2SeqLM.from_pretrained(MODEL_FR_EN)
|
| 17 |
+
translator_fr_en = pipeline("translation", model=model_fr_en, tokenizer=tokenizer_fr_en)
|
| 18 |
+
|
| 19 |
+
# Gemini API key (set as HF secret in the Space)
|
| 20 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 21 |
+
|
| 22 |
+
def translate_text(text, direction="en-fr"):
|
| 23 |
+
"""Translate using custom models, fallback to Gemini."""
|
| 24 |
+
try:
|
| 25 |
+
if direction == "en-fr":
|
| 26 |
+
return translator_en_fr(text, max_length=512)[0]['translation_text']
|
| 27 |
+
else:
|
| 28 |
+
return translator_fr_en(text, max_length=512)[0]['translation_text']
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print("⚠️ Local model failed, falling back to Gemini:", e)
|
| 31 |
+
return gemini_translate(text, direction)
|
| 32 |
+
|
| 33 |
+
def gemini_translate(text, direction="en-fr"):
|
| 34 |
+
"""Fallback using Gemini API."""
|
| 35 |
+
if not GEMINI_API_KEY:
|
| 36 |
+
return "Gemini API key not configured."
|
| 37 |
+
target = "fr" if direction == "en-fr" else "en"
|
| 38 |
+
response = requests.post(
|
| 39 |
+
"https://api.gemini.com/translate",
|
| 40 |
+
headers={"Authorization": f"Bearer {GEMINI_API_KEY}"},
|
| 41 |
+
json={"q": text, "target": target}
|
| 42 |
+
)
|
| 43 |
+
if response.status_code == 200:
|
| 44 |
+
return response.json().get("translatedText", "Translation failed.")
|
| 45 |
+
return f"Gemini API Error: {response.text}"
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
requests
|
| 6 |
+
python-dotenv
|
| 7 |
+
datasets
|