soniakhamitkar commited on
Commit
4f17c5b
·
verified ·
1 Parent(s): f7d4c76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -10
app.py CHANGED
@@ -17,22 +17,19 @@ def analyze_csv(file_path):
17
  results = df['text'].apply(lambda x: sentiment_pipeline(x)[0])
18
  df['sentiment'] = results.apply(lambda r: r['label'])
19
  df['score'] = results.apply(lambda r: r['score'])
20
-
21
- # Return the DataFrame as a CSV string
22
- return df.to_csv(index=False)
23
 
24
- def gradio_analyze(file_obj):
25
- # Get the path of the uploaded file and analyze it
26
- file_path = file_obj.name
27
- return analyze_csv(file_path)
28
 
29
  # Define the Gradio interface
30
  iface = gr.Interface(
31
- fn=gradio_analyze,
32
  inputs=gr.File(label="Upload CSV File", file_count="single", type="filepath"),
33
- outputs=gr.Textbox(label="CSV with Sentiment Analysis"),
34
  title="CSV Sentiment Analysis App",
35
- description="Upload a CSV file with a 'text' column. The app will run sentiment analysis on each row and return the CSV with sentiment labels and scores."
36
  )
37
 
38
  if __name__ == "__main__":
 
17
  results = df['text'].apply(lambda x: sentiment_pipeline(x)[0])
18
  df['sentiment'] = results.apply(lambda r: r['label'])
19
  df['score'] = results.apply(lambda r: r['score'])
 
 
 
20
 
21
+ # Save output to a new CSV file
22
+ output_csv_path = "output.csv"
23
+ df.to_csv(output_csv_path, index=False)
24
+ return output_csv_path # Return path to the new CSV
25
 
26
  # Define the Gradio interface
27
  iface = gr.Interface(
28
+ fn=analyze_csv,
29
  inputs=gr.File(label="Upload CSV File", file_count="single", type="filepath"),
30
+ outputs=gr.File(label="Download CSV File"),
31
  title="CSV Sentiment Analysis App",
32
+ description="Upload a CSV file with a 'text' column. The app will run sentiment analysis on each row and return a downloadable CSV with sentiment labels and scores."
33
  )
34
 
35
  if __name__ == "__main__":