Asad110786110 commited on
Commit
966b33f
·
verified ·
1 Parent(s): 5bd2340

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -45
app.py CHANGED
@@ -2,14 +2,14 @@ import os
2
  import pandas as pd
3
  import numpy as np
4
  import re
5
- from llm import call_llm
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
- prompt = """Analyze this image carefully for any unsafe or inappropriate content.
 
 
 
 
 
 
 
 
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
- verdict = call_llm(prompt, images=[img_base64], max_tokens=200, temperature=0.5),
50
- verdict_lower = verdict.lower()
51
- if "unsafe" in verdict_lower:
 
 
 
 
 
 
 
 
 
 
 
 
52
  return "unsafe"
53
 
54
  return "safe"
@@ -97,8 +117,14 @@ Review: {review_text}
97
  Sentiment:
98
  """
99
  try:
100
- raw = call_llm(prompt, max_tokens=500, temperature=0.2) # max_tokens 50–100 enough for sentiment
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
- app = FastAPI(
225
- title="Priceoye Review Moderation API",
226
- version="1.0"
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
- return {
249
- "sentiment": sentiment,
250
- "image_safety": image_flag,
251
- "decision": decision
252
- }
253
- if __name__ == "__main__":
254
- uvicorn.run(
255
- "app:app",
256
- host="0.0.0.0",
257
- port=7860,
258
- log_level="info"
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()