soufyane commited on
Commit
dd0ce30
1 Parent(s): 3f9e70e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -12
app.py CHANGED
@@ -22,34 +22,47 @@ def analyze_text(text):
22
 
23
  # Function to process a CSV file and update results live
24
  @spaces.GPU
 
 
 
 
 
 
 
25
  def analyze_csv(file):
26
- df = pd.read_csv(file.name, nrows=100)
27
  texts = df['text'].tolist()
28
-
29
  results = []
30
  pos_count = neg_count = neu_count = 0
31
-
32
  for text in texts:
33
  result = classifier(text)[0]
34
  results.append({'text': text, 'sentiment': result['label']})
35
-
36
  if result['label'] == 'positive':
37
  pos_count += 1
38
  elif result['label'] == 'negative':
39
  neg_count += 1
40
  else:
41
  neu_count += 1
42
-
43
  # Create a pie chart
44
  labels = 'Positive', 'Negative', 'Neutral'
45
  sizes = [pos_count, neg_count, neu_count]
46
  colors = ['#ff9999','#66b3ff','#99ff99']
47
  fig, ax = plt.subplots()
48
- wedges, texts, autotexts = ax.pie(sizes, labels=labels, colors=colors, autopct=lambda pct: "{:.1f}%\n({:d})".format(pct, int(pct/100.*sum(sizes))), startangle=90)
49
  ax.axis('equal')
50
-
51
  # Update results live
52
- yield pd.DataFrame(results), fig
 
 
 
 
 
 
53
 
54
  # Gradio interface
55
  with gr.Blocks() as demo:
@@ -59,16 +72,21 @@ with gr.Blocks() as demo:
59
  text_input = gr.Textbox(label="Enter Text")
60
  text_output = gr.JSON(label="Sentiment Analysis Result")
61
  text_button = gr.Button("Analyze Text")
62
-
63
  csv_input = gr.File(label="Upload CSV", file_types=['csv'])
64
  csv_output = gr.Dataframe(label="Sentiment Analysis Results")
65
  csv_button = gr.Button("Analyze CSV")
66
-
67
  with gr.Column():
 
 
 
 
68
  csv_chart = gr.Plot(label="Sentiment Distribution")
69
-
70
  text_button.click(analyze_text, inputs=text_input, outputs=text_output)
71
- csv_button.click(analyze_csv, inputs=csv_input, outputs=[csv_output, csv_chart])
 
72
 
73
  # Launch the Gradio app
74
  demo.launch()
 
22
 
23
  # Function to process a CSV file and update results live
24
  @spaces.GPU
25
+
26
+ # Function to process a single text input
27
+ def analyze_text(text):
28
+ result = classifier(text)[0]
29
+ return result
30
+
31
+ # Function to process a CSV file and update results live
32
  def analyze_csv(file):
33
+ df = pd.read_csv(file.name)
34
  texts = df['text'].tolist()
35
+
36
  results = []
37
  pos_count = neg_count = neu_count = 0
38
+
39
  for text in texts:
40
  result = classifier(text)[0]
41
  results.append({'text': text, 'sentiment': result['label']})
42
+
43
  if result['label'] == 'positive':
44
  pos_count += 1
45
  elif result['label'] == 'negative':
46
  neg_count += 1
47
  else:
48
  neu_count += 1
49
+
50
  # Create a pie chart
51
  labels = 'Positive', 'Negative', 'Neutral'
52
  sizes = [pos_count, neg_count, neu_count]
53
  colors = ['#ff9999','#66b3ff','#99ff99']
54
  fig, ax = plt.subplots()
55
+ ax.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
56
  ax.axis('equal')
57
+
58
  # Update results live
59
+ yield pd.DataFrame(results), fig, pos_count, neg_count, neu_count
60
+
61
+ # Function to save the DataFrame to a CSV file
62
+ def save_csv(df):
63
+ file_path = "/mnt/data/sentiment_analysis_results.csv"
64
+ df.to_csv(file_path, index=False)
65
+ return file_path
66
 
67
  # Gradio interface
68
  with gr.Blocks() as demo:
 
72
  text_input = gr.Textbox(label="Enter Text")
73
  text_output = gr.JSON(label="Sentiment Analysis Result")
74
  text_button = gr.Button("Analyze Text")
75
+
76
  csv_input = gr.File(label="Upload CSV", file_types=['csv'])
77
  csv_output = gr.Dataframe(label="Sentiment Analysis Results")
78
  csv_button = gr.Button("Analyze CSV")
79
+
80
  with gr.Column():
81
+ gr.Markdown("## csv Result")
82
+ pos_count_output = gr.Number(label="Positive Count", value=0)
83
+ neg_count_output = gr.Number(label="Negative Count", value=0)
84
+ neu_count_output = gr.Number(label="Neutral Count", value=0)
85
  csv_chart = gr.Plot(label="Sentiment Distribution")
86
+
87
  text_button.click(analyze_text, inputs=text_input, outputs=text_output)
88
+ csv_button.click(analyze_csv, inputs=csv_input, outputs=[csv_output, csv_chart, pos_count_output, neg_count_output, neu_count_output])
89
+ csv_button.click(fn=save_csv, inputs=csv_output, outputs=csv_download)
90
 
91
  # Launch the Gradio app
92
  demo.launch()