tahamueed23 commited on
Commit
ea7b9be
·
verified ·
1 Parent(s): 51ff677

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
3
+ from langdetect import detect
4
+ import pandas as pd
5
+ import os
6
+
7
+ # -----------------------------
8
+ # Load Models
9
+ # -----------------------------
10
+ # English sentiment model (CardiffNLP)
11
+ en_model = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
12
+
13
+ # Urdu / Roman Urdu fine-tuned model (replace with your Hugging Face repo ID)
14
+ ur_model_name = "tahamueed23/roman-urdu-sentiment"
15
+ ur_tokenizer = AutoTokenizer.from_pretrained(ur_model_name)
16
+ ur_model = AutoModelForSequenceClassification.from_pretrained(ur_model_name)
17
+ ur_pipeline = pipeline("sentiment-analysis", model=ur_model, tokenizer=ur_tokenizer)
18
+
19
+ # -----------------------------
20
+ # CSV Setup
21
+ # -----------------------------
22
+ csv_file = "sentiment_logs.csv"
23
+ if not os.path.exists(csv_file):
24
+ df = pd.DataFrame(columns=["Sentence", "Language", "Sentiment"])
25
+ df.to_csv(csv_file, index=False)
26
+
27
+ # -----------------------------
28
+ # Processing Function
29
+ # -----------------------------
30
+ def analyze_sentiment(sentence):
31
+ try:
32
+ # Detect language
33
+ lang = detect(sentence)
34
+
35
+ if lang == "en":
36
+ result = en_model(sentence)[0]
37
+ else:
38
+ # Treat anything non-English as Urdu/Roman Urdu
39
+ result = ur_pipeline(sentence)[0]
40
+
41
+ label = result["label"]
42
+ score = round(result["score"], 3)
43
+
44
+ # Save to CSV
45
+ new_row = pd.DataFrame([[sentence, lang, f"{label} ({score})"]],
46
+ columns=["Sentence", "Language", "Sentiment"])
47
+ df = pd.read_csv(csv_file)
48
+ df = pd.concat([df, new_row], ignore_index=True)
49
+ df.to_csv(csv_file, index=False)
50
+
51
+ # Output
52
+ return f"**Detected Language:** {lang}\n**Sentiment:** {label}\n**Confidence:** {score}"
53
+
54
+ except Exception as e:
55
+ return f"⚠️ Error: {str(e)}"
56
+
57
+ # -----------------------------
58
+ # Gradio App
59
+ # -----------------------------
60
+ with gr.Blocks() as demo:
61
+ gr.Markdown("## 🌍 Multilingual Sentiment Analysis (English, Urdu, Roman Urdu)")
62
+ gr.Markdown("Enter a sentence in **English, Urdu, or Roman Urdu** to detect sentiment.")
63
+
64
+ input_text = gr.Textbox(label="Enter your sentence:")
65
+ output_text = gr.Markdown(label="Result")
66
+
67
+ btn = gr.Button("Analyze Sentiment")
68
+ btn.click(analyze_sentiment, inputs=input_text, outputs=output_text)
69
+
70
+ demo.launch()