Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,11 +13,9 @@ OPENAI_API_URL = "https://models.inference.ai.azure.com/chat/completions"
|
|
| 13 |
OPENAI_MODEL_NAME = "gpt-4o-mini"
|
| 14 |
|
| 15 |
# 2. Google Gemini Configuration (Direct Google API)
|
| 16 |
-
# You need to set GOOGLE_API_KEY in your HF Space secrets
|
| 17 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", "")
|
| 18 |
|
| 19 |
-
# CORRECTED:
|
| 20 |
-
# NOTE: While this uses the "Gemini" API endpoint, it calls the Gemma 2 open model.
|
| 21 |
GEMINI_API_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemma-3-27b-it:generateContent?key={GOOGLE_API_KEY}"
|
| 22 |
|
| 23 |
app = FastAPI(
|
|
@@ -51,12 +49,8 @@ def home():
|
|
| 51 |
|
| 52 |
@app.get("/check-limit")
|
| 53 |
def check_limit():
|
| 54 |
-
"""
|
| 55 |
-
Checks the rate limit status of OpenAI tokens.
|
| 56 |
-
(Google API doesn't provide easy rate limit headers in the same way, skipped for now).
|
| 57 |
-
"""
|
| 58 |
if not AI_SERVICE_TOKENS:
|
| 59 |
-
# Just return empty if no OpenAI tokens, but don't crash if Google is used
|
| 60 |
return {"tokens_checked": 0, "results": [], "note": "OpenAI tokens missing"}
|
| 61 |
|
| 62 |
results = []
|
|
@@ -116,12 +110,12 @@ def call_google_gemini(filename):
|
|
| 116 |
if not GOOGLE_API_KEY:
|
| 117 |
raise Exception("GOOGLE_API_KEY not configured.")
|
| 118 |
|
| 119 |
-
#
|
| 120 |
prompt = f"""
|
| 121 |
You are an expert Movie and TV metadata analyst.
|
| 122 |
Analyze the filename: "{filename}"
|
| 123 |
Identify the title, year, and whether it is a series.
|
| 124 |
-
Return ONLY a raw JSON object with this exact format:
|
| 125 |
{{"title": "Movie Title", "year": "2024", "isSeries": false}}
|
| 126 |
"""
|
| 127 |
|
|
@@ -131,19 +125,17 @@ def call_google_gemini(filename):
|
|
| 131 |
}],
|
| 132 |
"generationConfig": {
|
| 133 |
"temperature": 0.1,
|
| 134 |
-
"maxOutputTokens": 100
|
| 135 |
-
"responseMimeType": "application/json"
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
-
# Note: The URL here uses the global GEMINI_API_URL defined at the top
|
| 140 |
response = requests.post(GEMINI_API_URL, headers={"Content-Type": "application/json"}, json=payload, timeout=30)
|
| 141 |
|
| 142 |
if response.status_code != 200:
|
| 143 |
raise Exception(f"Google Gemini API Error {response.status_code}: {response.text}")
|
| 144 |
|
| 145 |
result = response.json()
|
| 146 |
-
# Extract text from Gemini response structure
|
| 147 |
try:
|
| 148 |
return result['candidates'][0]['content']['parts'][0]['text']
|
| 149 |
except (KeyError, IndexError):
|
|
@@ -159,14 +151,11 @@ def analyze_filename(request: AnalyzeRequest):
|
|
| 159 |
|
| 160 |
try:
|
| 161 |
if provider_used == "gemma":
|
| 162 |
-
# Although the frontend sends "gemma", we map this to our Google Gemini function
|
| 163 |
raw_content = call_google_gemini(request.filename)
|
| 164 |
else:
|
| 165 |
-
# Default to OpenAI
|
| 166 |
if not AI_SERVICE_TOKENS: raise HTTPException(500, "OpenAI tokens missing.")
|
| 167 |
raw_content = call_openai_gpt4o(request.filename, AI_SERVICE_TOKENS)
|
| 168 |
|
| 169 |
-
# Parse JSON output from either provider
|
| 170 |
if raw_content:
|
| 171 |
# Clean up markdown code blocks if the model includes them
|
| 172 |
clean_content = raw_content.replace("```json", "").replace("```", "").strip()
|
|
|
|
| 13 |
OPENAI_MODEL_NAME = "gpt-4o-mini"
|
| 14 |
|
| 15 |
# 2. Google Gemini Configuration (Direct Google API)
|
|
|
|
| 16 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", "")
|
| 17 |
|
| 18 |
+
# CORRECTED: Use gemma-2-27b-it (Gemma 2).
|
|
|
|
| 19 |
GEMINI_API_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemma-3-27b-it:generateContent?key={GOOGLE_API_KEY}"
|
| 20 |
|
| 21 |
app = FastAPI(
|
|
|
|
| 49 |
|
| 50 |
@app.get("/check-limit")
|
| 51 |
def check_limit():
|
| 52 |
+
"""Checks the rate limit status of OpenAI tokens."""
|
|
|
|
|
|
|
|
|
|
| 53 |
if not AI_SERVICE_TOKENS:
|
|
|
|
| 54 |
return {"tokens_checked": 0, "results": [], "note": "OpenAI tokens missing"}
|
| 55 |
|
| 56 |
results = []
|
|
|
|
| 110 |
if not GOOGLE_API_KEY:
|
| 111 |
raise Exception("GOOGLE_API_KEY not configured.")
|
| 112 |
|
| 113 |
+
# Updated Prompt: Since we can't use JSON mode, we make the prompt stricter.
|
| 114 |
prompt = f"""
|
| 115 |
You are an expert Movie and TV metadata analyst.
|
| 116 |
Analyze the filename: "{filename}"
|
| 117 |
Identify the title, year, and whether it is a series.
|
| 118 |
+
Return ONLY a raw JSON object with this exact format (no markdown, no backticks):
|
| 119 |
{{"title": "Movie Title", "year": "2024", "isSeries": false}}
|
| 120 |
"""
|
| 121 |
|
|
|
|
| 125 |
}],
|
| 126 |
"generationConfig": {
|
| 127 |
"temperature": 0.1,
|
| 128 |
+
"maxOutputTokens": 100
|
| 129 |
+
# REMOVED: "responseMimeType": "application/json" (Not supported by Gemma)
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
|
|
|
| 133 |
response = requests.post(GEMINI_API_URL, headers={"Content-Type": "application/json"}, json=payload, timeout=30)
|
| 134 |
|
| 135 |
if response.status_code != 200:
|
| 136 |
raise Exception(f"Google Gemini API Error {response.status_code}: {response.text}")
|
| 137 |
|
| 138 |
result = response.json()
|
|
|
|
| 139 |
try:
|
| 140 |
return result['candidates'][0]['content']['parts'][0]['text']
|
| 141 |
except (KeyError, IndexError):
|
|
|
|
| 151 |
|
| 152 |
try:
|
| 153 |
if provider_used == "gemma":
|
|
|
|
| 154 |
raw_content = call_google_gemini(request.filename)
|
| 155 |
else:
|
|
|
|
| 156 |
if not AI_SERVICE_TOKENS: raise HTTPException(500, "OpenAI tokens missing.")
|
| 157 |
raw_content = call_openai_gpt4o(request.filename, AI_SERVICE_TOKENS)
|
| 158 |
|
|
|
|
| 159 |
if raw_content:
|
| 160 |
# Clean up markdown code blocks if the model includes them
|
| 161 |
clean_content = raw_content.replace("```json", "").replace("```", "").strip()
|