esocoder commited on
Commit
bad1f5f
1 Parent(s): 0a0c99d
Files changed (1) hide show
  1. individual_analyzes.py +31 -13
individual_analyzes.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  from transformers import pipeline
3
  from utils import read_poems_from_directory
4
  import matplotlib.pyplot as plt
@@ -6,6 +7,10 @@ import matplotlib.pyplot as plt
6
  sentiment_classifier = pipeline("sentiment-analysis")
7
  emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
8
 
 
 
 
 
9
  def analyze_poem(poem):
10
  sentiment = sentiment_classifier(poem)[0]
11
  emotion_scores = emotion_classifier(poem)[0]
@@ -26,22 +31,35 @@ def plot_emotion_scores(emotion_scores):
26
  return fig
27
 
28
  def analyze_individual_page():
29
- st.header("Analyze Poems")
30
- poems_directory = "poems"
31
-
 
 
 
 
 
 
 
 
 
 
 
 
32
  poems = read_poems_from_directory(poems_directory)
33
 
34
  if poems:
35
- for i, poem in enumerate(poems, start=1):
36
- st.subheader(f"Poem {i}")
37
- st.text(poem)
38
- sentiment, emotion, emotion_scores = analyze_poem(poem)
39
- st.write(f"Sentiment: {sentiment['label']} (score: {sentiment['score']:.2f})")
40
- st.write(f"Emotion: {emotion}")
41
-
42
- # Plot emotion scores
43
- fig = plot_emotion_scores(emotion_scores)
44
- st.pyplot(fig)
 
45
  else:
46
  st.warning("No poems found in the 'poems' directory.")
47
 
 
1
  import streamlit as st
2
+ import os
3
  from transformers import pipeline
4
  from utils import read_poems_from_directory
5
  import matplotlib.pyplot as plt
 
7
  sentiment_classifier = pipeline("sentiment-analysis")
8
  emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
9
 
10
+ # Directory for poems
11
+ poems_directory = "./poems"
12
+ os.makedirs(poems_directory, exist_ok=True)
13
+
14
  def analyze_poem(poem):
15
  sentiment = sentiment_classifier(poem)[0]
16
  emotion_scores = emotion_classifier(poem)[0]
 
31
  return fig
32
 
33
  def analyze_individual_page():
34
+ st.header("Analyze Individual Poems")
35
+
36
+ # Sidebar for file upload and listing files
37
+ st.sidebar.title("Upload New Poem")
38
+ uploaded_file = st.sidebar.file_uploader("Choose a text file", type="txt")
39
+
40
+ if uploaded_file is not None:
41
+ with open(os.path.join(poems_directory, uploaded_file.name), "wb") as f:
42
+ f.write(uploaded_file.getbuffer())
43
+ st.sidebar.success(f"Uploaded {uploaded_file.name}")
44
+
45
+ st.sidebar.title("Available Poems")
46
+ poem_files = [f for f in os.listdir(poems_directory) if f.endswith(".txt")]
47
+ st.sidebar.write("\n".join(poem_files))
48
+
49
  poems = read_poems_from_directory(poems_directory)
50
 
51
  if poems:
52
+ selected_poem = st.selectbox("Select a poem to analyze", range(len(poems)), format_func=lambda i: f"Poem {i+1}")
53
+
54
+ st.subheader(f"Analyzing Poem {selected_poem + 1}")
55
+ st.text(poems[selected_poem])
56
+ sentiment, emotion, emotion_scores = analyze_poem(poems[selected_poem])
57
+ st.write(f"Sentiment: {sentiment['label']} (score: {sentiment['score']:.2f})")
58
+ st.write(f"Dominant Emotion: {emotion}")
59
+
60
+ # Plot emotion scores
61
+ fig = plot_emotion_scores(emotion_scores)
62
+ st.pyplot(fig)
63
  else:
64
  st.warning("No poems found in the 'poems' directory.")
65