Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install gradio transformers requests
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
import requests
|
6 |
+
import time # For simulating intermediate steps
|
7 |
+
|
8 |
+
# Load the sentiment analysis model
|
9 |
+
classifier = pipeline('sentiment-analysis', model='krishnamishra8848/movie_sentiment_analysis')
|
10 |
+
|
11 |
+
# Language detection function
|
12 |
+
def detect_language(text):
|
13 |
+
detect_url = "https://google-translator9.p.rapidapi.com/v2/detect"
|
14 |
+
detect_payload = {"q": text}
|
15 |
+
headers = {
|
16 |
+
"x-rapidapi-key": "ef532cb7b6msh96f36c918327aacp171ce5jsn42c4de22fe5d",
|
17 |
+
"x-rapidapi-host": "google-translator9.p.rapidapi.com",
|
18 |
+
"Content-Type": "application/json"
|
19 |
+
}
|
20 |
+
|
21 |
+
response = requests.post(detect_url, json=detect_payload, headers=headers)
|
22 |
+
if response.status_code == 200:
|
23 |
+
detections = response.json().get('data', {}).get('detections', [[]])[0]
|
24 |
+
if detections:
|
25 |
+
return detections[0].get('language')
|
26 |
+
return None
|
27 |
+
|
28 |
+
# Translation function
|
29 |
+
def translate_text(text, source_language, target_language="en"):
|
30 |
+
translate_url = "https://google-translator9.p.rapidapi.com/v2"
|
31 |
+
translate_payload = {
|
32 |
+
"q": text,
|
33 |
+
"source": source_language,
|
34 |
+
"target": target_language,
|
35 |
+
"format": "text"
|
36 |
+
}
|
37 |
+
headers = {
|
38 |
+
"x-rapidapi-key": "ef532cb7b6msh96f36c918327aacp171ce5jsn42c4de22fe5d",
|
39 |
+
"x-rapidapi-host": "google-translator9.p.rapidapi.com",
|
40 |
+
"Content-Type": "application/json"
|
41 |
+
}
|
42 |
+
|
43 |
+
response = requests.post(translate_url, json=translate_payload, headers=headers)
|
44 |
+
if response.status_code == 200:
|
45 |
+
translations = response.json().get('data', {}).get('translations', [{}])
|
46 |
+
if translations:
|
47 |
+
return translations[0].get('translatedText')
|
48 |
+
return None
|
49 |
+
|
50 |
+
# Main function for Gradio
|
51 |
+
def analyze_sentiment_with_steps(text):
|
52 |
+
# Step 1: Detecting Language
|
53 |
+
status = "Detecting Language..."
|
54 |
+
yield status
|
55 |
+
detected_language = detect_language(text)
|
56 |
+
if not detected_language:
|
57 |
+
yield "Error: Could not detect the language."
|
58 |
+
return
|
59 |
+
|
60 |
+
status = f"Language Detected: {detected_language.upper()}"
|
61 |
+
yield status
|
62 |
+
|
63 |
+
# Step 2: Translating if necessary
|
64 |
+
if detected_language != "en":
|
65 |
+
status += "\nTranslating text to English..."
|
66 |
+
yield status
|
67 |
+
text = translate_text(text, detected_language)
|
68 |
+
if not text:
|
69 |
+
yield "Error: Could not translate the input text."
|
70 |
+
return
|
71 |
+
|
72 |
+
# Step 3: Sending to model
|
73 |
+
status += "\nSending to Model..."
|
74 |
+
yield status
|
75 |
+
time.sleep(1) # Simulate processing time for better user experience
|
76 |
+
|
77 |
+
# Step 4: Sentiment analysis
|
78 |
+
result = classifier(text)
|
79 |
+
label_mapping = {"LABEL_0": "negative", "LABEL_1": "positive"}
|
80 |
+
sentiment = label_mapping[result[0]['label']]
|
81 |
+
confidence = result[0]['score']
|
82 |
+
status += f"\nPrediction: {sentiment.capitalize()} (Confidence: {confidence:.2f})"
|
83 |
+
yield status
|
84 |
+
|
85 |
+
# Gradio interface
|
86 |
+
interface = gr.Interface(
|
87 |
+
fn=analyze_sentiment_with_steps,
|
88 |
+
inputs=gr.Textbox(
|
89 |
+
label="Enter Movie Review",
|
90 |
+
placeholder="Type your review in any language...",
|
91 |
+
lines=3
|
92 |
+
),
|
93 |
+
outputs=gr.Textbox(label="Prediction Steps"),
|
94 |
+
live=True,
|
95 |
+
title="Multilingual Movie Sentiment Analysis",
|
96 |
+
description=(
|
97 |
+
"This app analyzes movie reviews written in any language. "
|
98 |
+
"It detects the language, translates it to English (if required), "
|
99 |
+
"and predicts the sentiment (positive/negative)."
|
100 |
+
)
|
101 |
+
)
|
102 |
+
|
103 |
+
# Launch Gradio app
|
104 |
+
interface.launch(share=True)
|