Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
|
| 7 |
+
# OpenRouter API client (API key ko Hugging Face Space ke secrets mein add karna hai)
|
| 8 |
+
client = OpenAI(
|
| 9 |
+
base_url="https://openrouter.ai/api/v1",
|
| 10 |
+
api_key=os.getenv("OPENROUTER_API_KEY") # HF Secret
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# --- Sentiment Function ---
|
| 14 |
+
def get_sentiment(review_text: str) -> str:
|
| 15 |
+
prompt = f"""
|
| 16 |
+
Classify sentiment of this review in English or Roman Urdu.
|
| 17 |
+
Return EXACTLY one word: positive, negative, or neutral.
|
| 18 |
+
|
| 19 |
+
Rules (priority order, MUST follow strictly):
|
| 20 |
+
|
| 21 |
+
1. If the review mentions problems with Priceoye's website, service, delivery,
|
| 22 |
+
shipping time, support, or Priceoye as a company/brand overall,
|
| 23 |
+
ALWAYS respond with "negative".
|
| 24 |
+
This overrides everything else, even if the review also praises products.
|
| 25 |
+
|
| 26 |
+
2. If both positive and negative opinions are present but not directly about
|
| 27 |
+
Priceoye's website, service, or delivery → respond "neutral".
|
| 28 |
+
|
| 29 |
+
3. Otherwise:
|
| 30 |
+
- Respond "positive" if the review is praising.
|
| 31 |
+
- Respond "negative" if the review is complaining.
|
| 32 |
+
- Respond "neutral" if it is neither.
|
| 33 |
+
|
| 34 |
+
4. respond with only one word.
|
| 35 |
+
|
| 36 |
+
Do NOT explain. Do NOT add anything else.
|
| 37 |
+
|
| 38 |
+
Review: {review_text}
|
| 39 |
+
Sentiment:
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
response = client.chat.completions.create(
|
| 44 |
+
model="openai/gpt-5-mini",
|
| 45 |
+
messages=[{"role": "user", "content": prompt}],
|
| 46 |
+
temperature=0.2,
|
| 47 |
+
max_tokens=10
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
raw = response.choices[0].message.content or ""
|
| 51 |
+
raw_lower = raw.strip().lower()
|
| 52 |
+
|
| 53 |
+
if "positive" in raw_lower:
|
| 54 |
+
return "positive"
|
| 55 |
+
elif "negative" in raw_lower:
|
| 56 |
+
return "negative"
|
| 57 |
+
elif "neutral" in raw_lower:
|
| 58 |
+
return "neutral"
|
| 59 |
+
else:
|
| 60 |
+
return "neutral"
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print("Error from API:", str(e))
|
| 64 |
+
return "neutral"
|
| 65 |
+
|
| 66 |
+
# --- Validation Function (short version) ---
|
| 67 |
+
def validate_review(description, rating):
|
| 68 |
+
LLM = get_sentiment(description)
|
| 69 |
+
|
| 70 |
+
data = pd.DataFrame({
|
| 71 |
+
'LLM': [LLM],
|
| 72 |
+
'description': [description],
|
| 73 |
+
'rating': [rating]
|
| 74 |
+
})
|
| 75 |
+
|
| 76 |
+
# Simplified rules (your full rules can be pasted here if needed)
|
| 77 |
+
if rating in ["4", "5"] and LLM == "positive":
|
| 78 |
+
decision = "accepted"
|
| 79 |
+
elif rating in ["1", "2"] and LLM == "negative":
|
| 80 |
+
decision = "rejected"
|
| 81 |
+
else:
|
| 82 |
+
decision = "ignored"
|
| 83 |
+
|
| 84 |
+
return LLM, decision
|
| 85 |
+
|
| 86 |
+
# --- Gradio UI ---
|
| 87 |
+
def classify_review(description, rating):
|
| 88 |
+
sentiment, decision = validate_review(description, rating)
|
| 89 |
+
return sentiment, decision
|
| 90 |
+
|
| 91 |
+
with gr.Blocks() as demo:
|
| 92 |
+
gr.Markdown("## 📊 Review Sentiment & Validation Tool")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
review_input = gr.Textbox(label="Enter Review", placeholder="Write review in English or Roman Urdu...")
|
| 96 |
+
rating_input = gr.Dropdown(choices=["1","2","3","4","5"], label="Rating (1-5)", value="5")
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
sentiment_output = gr.Textbox(label="Predicted Sentiment")
|
| 100 |
+
decision_output = gr.Textbox(label="Final Decision")
|
| 101 |
+
|
| 102 |
+
btn = gr.Button("Classify Review")
|
| 103 |
+
btn.click(fn=classify_review, inputs=[review_input, rating_input], outputs=[sentiment_output, decision_output])
|
| 104 |
+
|
| 105 |
+
demo.launch()
|