Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,14 @@ import os
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import re
|
| 5 |
-
|
| 6 |
from openai import OpenAI
|
| 7 |
import base64
|
| 8 |
-
from fastapi import FastAPI, UploadFile, File, Form
|
| 9 |
-
from typing import List, Optional
|
| 10 |
-
import uvicorn
|
| 11 |
-
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# ---------------- IMAGE MODERATION FUNCTION ---------------- #
|
| 15 |
def check_image_safety(image_paths):
|
|
@@ -29,7 +29,15 @@ def check_image_safety(image_paths):
|
|
| 29 |
with open(image_path, "rb") as img_file:
|
| 30 |
img_base64 = base64.b64encode(img_file.read()).decode("utf-8")
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
You must respond with EXACTLY ONE WORD: 'safe' or 'unsafe'. Do NOT explain.
|
| 35 |
|
|
@@ -46,9 +54,21 @@ Remember:
|
|
| 46 |
|
| 47 |
Return only: safe OR unsafe.
|
| 48 |
"""
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return "unsafe"
|
| 53 |
|
| 54 |
return "safe"
|
|
@@ -97,8 +117,14 @@ Review: {review_text}
|
|
| 97 |
Sentiment:
|
| 98 |
"""
|
| 99 |
try:
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
raw_lower = raw.lower().strip()
|
| 103 |
|
| 104 |
if "positive" in raw_lower:
|
|
@@ -221,40 +247,24 @@ def validate_review(description, rating, image_paths=None):
|
|
| 221 |
return data['Labeled Result'][0], LLM, image_flag
|
| 222 |
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
)
|
|
|
|
| 228 |
|
| 229 |
-
@app.post("/classify-review")
|
| 230 |
-
async def classify_review(
|
| 231 |
-
description: str = Form(""),
|
| 232 |
-
rating: str = Form(...),
|
| 233 |
-
images: Optional[List[UploadFile]] = File(None)
|
| 234 |
-
):
|
| 235 |
-
image_paths = []
|
| 236 |
-
|
| 237 |
-
if images:
|
| 238 |
-
for img in images[:3]: # max 3 images
|
| 239 |
-
file_path = f"/tmp/{img.filename}"
|
| 240 |
-
with open(file_path, "wb") as f:
|
| 241 |
-
f.write(await img.read())
|
| 242 |
-
image_paths.append(file_path)
|
| 243 |
-
|
| 244 |
-
decision, sentiment, image_flag = validate_review(
|
| 245 |
-
description, rating, image_paths
|
| 246 |
-
)
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
"
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
)
|
| 260 |
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import re
|
| 5 |
+
import gradio as gr
|
| 6 |
from openai import OpenAI
|
| 7 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
client = OpenAI(
|
| 10 |
+
base_url="https://openrouter.ai/api/v1",
|
| 11 |
+
api_key=os.getenv("OPENROUTER_API_KEY")
|
| 12 |
+
)
|
| 13 |
|
| 14 |
# ---------------- IMAGE MODERATION FUNCTION ---------------- #
|
| 15 |
def check_image_safety(image_paths):
|
|
|
|
| 29 |
with open(image_path, "rb") as img_file:
|
| 30 |
img_base64 = base64.b64encode(img_file.read()).decode("utf-8")
|
| 31 |
|
| 32 |
+
response = client.chat.completions.create(
|
| 33 |
+
model="openai/gpt-5-mini",
|
| 34 |
+
messages=[
|
| 35 |
+
{
|
| 36 |
+
"role": "user",
|
| 37 |
+
"content": [
|
| 38 |
+
{
|
| 39 |
+
"type": "text",
|
| 40 |
+
"text": """Analyze this image carefully for any unsafe or inappropriate content.
|
| 41 |
|
| 42 |
You must respond with EXACTLY ONE WORD: 'safe' or 'unsafe'. Do NOT explain.
|
| 43 |
|
|
|
|
| 54 |
|
| 55 |
Return only: safe OR unsafe.
|
| 56 |
"""
|
| 57 |
+
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"type": "image_url",
|
| 61 |
+
"image_url": f"data:image/jpeg;base64,{img_base64}"
|
| 62 |
+
}
|
| 63 |
+
]
|
| 64 |
+
}
|
| 65 |
+
],
|
| 66 |
+
temperature=0.5,
|
| 67 |
+
max_tokens=200,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
verdict = response.choices[0].message.content.strip().lower()
|
| 71 |
+
if "unsafe" in verdict:
|
| 72 |
return "unsafe"
|
| 73 |
|
| 74 |
return "safe"
|
|
|
|
| 117 |
Sentiment:
|
| 118 |
"""
|
| 119 |
try:
|
| 120 |
+
response = client.chat.completions.create(
|
| 121 |
+
model="openai/gpt-5-mini",
|
| 122 |
+
messages=[{"role": "user", "content": prompt}],
|
| 123 |
+
temperature=0.2,
|
| 124 |
+
max_tokens=500
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
raw = response.choices[0].message.content or ""
|
| 128 |
raw_lower = raw.lower().strip()
|
| 129 |
|
| 130 |
if "positive" in raw_lower:
|
|
|
|
| 247 |
return data['Labeled Result'][0], LLM, image_flag
|
| 248 |
|
| 249 |
|
| 250 |
+
# ---------------- GRADIO INTERFACE ---------------- #
|
| 251 |
+
def classify_review(description, rating, images):
|
| 252 |
+
image_paths = [img.name for img in images] if images else None
|
| 253 |
+
result, sentiment, image_flag = validate_review(description, rating, image_paths)
|
| 254 |
+
return f"Sentiment: {sentiment}\nImage Safety: {image_flag}\nDecision: {result}"
|
| 255 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
+
iface = gr.Interface(
|
| 258 |
+
fn=classify_review,
|
| 259 |
+
inputs=[
|
| 260 |
+
gr.Textbox(label="Review Description"),
|
| 261 |
+
gr.Radio(["1", "2", "3", "4", "5"], label="Rating"),
|
| 262 |
+
gr.Files(label="Upload Images (optional)")
|
| 263 |
+
],
|
| 264 |
+
outputs="text",
|
| 265 |
+
title="Priceoye Review Classifier (with Image Moderation)",
|
| 266 |
+
description="Classifies reviews as accepted/rejected/ignored using LLM + rule logic + image safety check."
|
| 267 |
+
)
|
|
|
|
| 268 |
|
| 269 |
+
if __name__ == "__main__":
|
| 270 |
+
iface.launch()
|