Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,65 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
-
from
|
4 |
-
import
|
5 |
-
from PIL import Image
|
6 |
|
7 |
# Load models
|
8 |
def load_models():
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
# Process image to text
|
19 |
-
def generate_caption(image, image_to_text):
|
20 |
-
result = image_to_text(image)
|
21 |
-
return result[0]["generated_text"] if result else "No caption generated."
|
22 |
|
23 |
# Generate a narrative story using the GPT-2 genre-based story generator
|
24 |
-
def
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Main Streamlit app
|
38 |
def main():
|
39 |
-
st.title("AI-Powered
|
40 |
|
41 |
-
|
42 |
|
43 |
-
|
44 |
|
45 |
-
if
|
46 |
-
#
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
49 |
|
50 |
-
with st.spinner("
|
51 |
-
|
52 |
-
st.write("###
|
53 |
-
st.write(caption)
|
54 |
-
|
55 |
-
with st.spinner("Generating story..."):
|
56 |
-
story = generate_story(caption, storyteller)
|
57 |
-
st.write("### Generated Story:")
|
58 |
st.write(story)
|
59 |
|
60 |
-
with st.spinner("Generating speech..."):
|
61 |
-
audio_file = text_to_speech(story)
|
62 |
-
st.audio(audio_file, format="audio/mp3")
|
63 |
-
|
64 |
if __name__ == "__main__":
|
65 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
+
from googletrans import Translator
|
4 |
+
import time
|
|
|
5 |
|
6 |
# Load models
|
7 |
def load_models():
|
8 |
+
sentiment_analyzer = pipeline("text-classification", model="miltonc/distilbert-base-uncased_ft_5")
|
9 |
+
summarizer = pipeline("summarization", model="FelixChao/T5-Chinese-Summarization")
|
10 |
+
return sentiment_analyzer, summarizer
|
11 |
+
|
12 |
+
|
13 |
+
def sentiment_analysis(text, sentiment_analyzer):
|
14 |
+
try:
|
15 |
+
result = sentiment_analyzer(text)[0]["generated_text"] #Adjusted max and min lengths.
|
16 |
+
return result
|
17 |
+
except Exception as e:
|
18 |
+
print(f"sentiment_analysis error for '{text}': {e}. Returning 'sentiment_analysis Failed'")
|
19 |
+
return "sentiment_analysis Failed"
|
20 |
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Generate a narrative story using the GPT-2 genre-based story generator
|
23 |
+
def summarize_news(text, summarizer):
|
24 |
+
try:
|
25 |
+
summary = summarizer(text, max_length=30, min_length=10)[0]['summary_text']
|
26 |
+
return summary
|
27 |
+
except Exception as e:
|
28 |
+
print(f"Summarization error for '{text}': {e}. Returning 'Summarization Failed'")
|
29 |
+
return "Summarization Failed"
|
30 |
+
|
31 |
|
32 |
+
def translate_text(text_to_translate, target_language='en', source_language='zh-TW', delay=1):
|
33 |
+
translator = Translator()
|
34 |
+
try:
|
35 |
+
translation = translator.translate(text_to_translate, dest=target_language, src=source_language)
|
36 |
+
time.sleep(delay) # Add a delay to avoid rate limiting.
|
37 |
+
return translation.text
|
38 |
+
except Exception as e:
|
39 |
+
print(f"Translation error for '{text_to_translate}': {e}. Returning 'Translation Failed'")
|
40 |
+
time.sleep(delay)
|
41 |
+
return "Translation Failed"
|
42 |
|
43 |
# Main Streamlit app
|
44 |
def main():
|
45 |
+
st.title("AI-Powered Sentiment Analysis and Summarization")
|
46 |
|
47 |
+
sentiment_analyzer, summarizer = load_models()
|
48 |
|
49 |
+
text = st.text_area("Enter the Chinese text here.....", height=200) # Changed from file_uploader to text_area
|
50 |
|
51 |
+
if text: # check if text is not empty
|
52 |
+
# google translate package
|
53 |
+
with st.spinner("Analyzing sentiment..."):
|
54 |
+
text_en = translate_text(text, target_language='en', source_language='zh-TW', delay=1)
|
55 |
+
sentiment_output = sentiment_analysis(text_en, sentiment_analyzer)
|
56 |
+
st.write("### Sentiment:")
|
57 |
+
st.write(sentiment_output)
|
58 |
|
59 |
+
with st.spinner("Summarizing News..."):
|
60 |
+
story = summarize_news(text, summarizer)
|
61 |
+
st.write("### Summarized News:")
|
|
|
|
|
|
|
|
|
|
|
62 |
st.write(story)
|
63 |
|
|
|
|
|
|
|
|
|
64 |
if __name__ == "__main__":
|
65 |
main()
|