teymoor commited on
Commit
405e368
·
1 Parent(s): 6e68edd

fix(model): Replace Farsi model with a public one

Browse files
Files changed (1) hide show
  1. app.py +57 -36
app.py CHANGED
@@ -2,46 +2,67 @@
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
- # Load the sentiment analysis pipeline
6
- # This will download the model and tokenizer for the first time.
7
- # The model is 'distilbert-base-uncased-finetuned-sst-2-english'.
8
- # It's a lightweight and fast model fine-tuned for sentiment analysis.
9
- print("Loading sentiment analysis pipeline...")
10
- sentiment_pipeline = pipeline("sentiment-analysis")
11
- print("Pipeline loaded successfully.")
12
-
13
- # Define the function that will perform the sentiment analysis
14
- def analyze_sentiment(text):
 
 
 
15
  """
16
- Analyzes the sentiment of a given text.
17
-
18
- Args:
19
- text (str): The input text from the user.
20
-
21
- Returns:
22
- str: A formatted string with the prediction and score.
 
 
 
 
 
 
23
  """
24
- if text:
25
- # The pipeline returns a list of dictionaries.
26
- # e.g., [{'label': 'POSITIVE', 'score': 0.999}]
27
- result = sentiment_pipeline(text)[0]
28
- label = result['label']
29
- score = result['score']
30
-
31
- # Format the output for better readability
32
- return f"Sentiment: {label} (Score: {score:.4f})"
33
- else:
34
  return "Please enter some text to analyze."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- # Create the Gradio interface for the application
37
- iface = gr.Interface(
38
- fn=analyze_sentiment,
39
- inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
40
- outputs="text",
41
- title="Simple Sentiment Analyzer",
42
- description="Enter any English text to find out if its sentiment is POSITIVE or NEGATIVE. Powered by Hugging Face Transformers.",
43
- allow_flagging="never" # Disables the flagging feature
44
- )
45
 
46
  # Launch the web application
47
  if __name__ == "__main__":
 
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
+ # --- Configuration ---
6
+ # Define the models we want to offer in our application.
7
+ # We are now using a confirmed, publicly available Persian model.
8
+ MODELS = {
9
+ "English": "distilbert-base-uncased-finetuned-sst-2-english",
10
+ "Persian (Farsi)": "m3hrdadfi/albert-fa-base-v2-sentiment-binary",
11
+ }
12
+
13
+ # --- State and Caching ---
14
+ # Create a dictionary to cache the loaded pipelines.
15
+ pipeline_cache = {}
16
+
17
+ def get_pipeline(model_name):
18
  """
19
+ Loads a sentiment-analysis pipeline for the given model name.
20
+ Uses a cache to avoid reloading models that are already in memory.
21
+ """
22
+ if model_name not in pipeline_cache:
23
+ print(f"Loading pipeline for model: {model_name}...")
24
+ pipeline_cache[model_name] = pipeline("sentiment-analysis", model=model_name)
25
+ print("Pipeline loaded successfully.")
26
+ return pipeline_cache[model_name]
27
+
28
+ # --- Core Logic ---
29
+ def analyze_sentiment(text, model_choice):
30
+ """
31
+ Analyzes the sentiment of a given text using the selected model.
32
  """
33
+ if not text:
 
 
 
 
 
 
 
 
 
34
  return "Please enter some text to analyze."
35
+
36
+ model_name = MODELS[model_choice]
37
+ sentiment_pipeline = get_pipeline(model_name)
38
+
39
+ result = sentiment_pipeline(text)[0]
40
+ label = result['label']
41
+ score = result['score']
42
+
43
+ return f"Sentiment: {label} (Score: {score:.4f})"
44
+
45
+ # --- Gradio Interface ---
46
+ with gr.Blocks() as iface:
47
+ gr.Markdown("# Multi-Lingual Sentiment Analyzer")
48
+ gr.Markdown("Select a language, enter some text, and see the sentiment analysis. The first time you select a language, the model will take a moment to load.")
49
+
50
+ with gr.Row():
51
+ model_selector = gr.Dropdown(
52
+ choices=list(MODELS.keys()),
53
+ value="English",
54
+ label="Select Language Model"
55
+ )
56
+ output_text = gr.Textbox(label="Result", interactive=False)
57
+
58
+ input_text = gr.Textbox(lines=5, placeholder="Enter text here...")
59
+ submit_button = gr.Button("Analyze Sentiment")
60
 
61
+ submit_button.click(
62
+ fn=analyze_sentiment,
63
+ inputs=[input_text, model_selector],
64
+ outputs=output_text
65
+ )
 
 
 
 
66
 
67
  # Launch the web application
68
  if __name__ == "__main__":