tahamueed23 commited on
Commit
dd037e8
·
verified ·
1 Parent(s): cced859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -1,6 +1,5 @@
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
 
@@ -27,29 +26,26 @@ if not os.path.exists(csv_file):
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)}"
@@ -59,12 +55,13 @@ def analyze_sentiment(sentence):
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()
 
1
  import gradio as gr
2
  from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
 
3
  import pandas as pd
4
  import os
5
 
 
26
  # -----------------------------
27
  # Processing Function
28
  # -----------------------------
29
+ def analyze_sentiment(sentence, language):
30
  try:
31
+ if language == "English":
 
 
 
32
  result = en_model(sentence)[0]
33
  else:
34
+ # Urdu or Roman Urdu
35
  result = ur_pipeline(sentence)[0]
36
 
37
  label = result["label"]
38
  score = round(result["score"], 3)
39
 
40
  # Save to CSV
41
+ new_row = pd.DataFrame([[sentence, language, f"{label} ({score})"]],
42
  columns=["Sentence", "Language", "Sentiment"])
43
  df = pd.read_csv(csv_file)
44
  df = pd.concat([df, new_row], ignore_index=True)
45
  df.to_csv(csv_file, index=False)
46
 
47
  # Output
48
+ return f"**Language Selected:** {language}\n**Sentiment:** {label}\n**Confidence:** {score}"
49
 
50
  except Exception as e:
51
  return f"⚠️ Error: {str(e)}"
 
55
  # -----------------------------
56
  with gr.Blocks() as demo:
57
  gr.Markdown("## 🌍 Multilingual Sentiment Analysis (English, Urdu, Roman Urdu)")
58
+ gr.Markdown("Enter a sentence and select the language to detect sentiment.")
59
 
60
  input_text = gr.Textbox(label="Enter your sentence:")
61
+ lang_dropdown = gr.Dropdown(choices=["English", "Urdu", "Roman Urdu"], label="Select Language")
62
  output_text = gr.Markdown(label="Result")
63
 
64
  btn = gr.Button("Analyze Sentiment")
65
+ btn.click(analyze_sentiment, inputs=[input_text, lang_dropdown], outputs=output_text)
66
 
67
  demo.launch()