felixwf commited on
Commit
dae8411
β€’
1 Parent(s): b063516

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -46
app.py CHANGED
@@ -1,63 +1,101 @@
1
  import streamlit as st
2
  from transformers import pipeline
 
 
 
 
3
 
4
- # img2text
5
- def img2text(url):
6
- image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
7
- text = image_to_text_model(url)[0]["generated_text"]
8
 
9
- print(text)
10
- return text
11
 
12
- # txt2Story
13
- def txt2story(text):
14
- pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
15
- story_txt = pipe(text)[0]['generated_text']
16
 
17
- print(story_txt)
18
- return story_txt
 
 
 
 
19
 
20
- # Story2Audio
21
- def text2audio(story_text):
22
- pipe = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
23
- audio_data = pipe(story_text)
24
- return audio_data
25
 
 
 
 
 
 
 
26
 
27
- def main():
28
- st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
29
- st.header("Turn Your Image to Audio Story")
30
- uploaded_file = st.file_uploader("Select an Image...")
31
 
32
- if uploaded_file is not None:
33
- print(uploaded_file)
34
- bytes_data = uploaded_file.getvalue()
35
- with open(uploaded_file.name, "wb") as file:
36
- file.write(bytes_data)
37
- st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
38
 
39
 
40
- #Stage 1: Image to Text
41
- st.text('Processing img2text...')
42
- scenario = img2text(uploaded_file.name)
43
- st.write(scenario)
44
 
45
- #Stage 2: Text to Story
46
- st.text('Generating a story...')
47
- story = txt2story(scenario)
48
- st.write(story)
49
 
50
- #Stage 3: Story to Audio data
51
- st.text('Generating audio data...')
52
- audio_data =text2audio(story)
 
53
 
54
- # Play button
55
- if st.button("Play Audio"):
56
- st.audio(audio_data['audio'],
57
- format="audio/wav",
58
- start_time=0,
59
- sample_rate = audio_data['sampling_rate'])
60
 
 
 
 
 
61
 
62
- if __name__ == "__main__":
63
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ from transformers import AutoModelForSequenceClassification
4
+ from transformers import AutoTokenizer
5
+ import torch
6
+ import numpy as np
7
 
8
+ def main():
 
 
 
9
 
 
 
10
 
11
+ st.title("yelp2024fall Test")
12
+ st.write("Enter a sentence for analysis:")
 
 
13
 
14
+ user_input = st.text_input("")
15
+ if user_input:
16
+ # Approach: AutoModel
17
+ model2 = AutoModelForSequenceClassification.from_pretrained("isom5240/CustomModel_yelp2024fall",
18
+ num_labels=5)
19
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
20
 
21
+ inputs = tokenizer(user_input,
22
+ padding=True,
23
+ truncation=True,
24
+ return_tensors='pt')
 
25
 
26
+ outputs = model2(**inputs)
27
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
28
+ predictions = predictions.cpu().detach().numpy()
29
+ # Get the index of the largest output value
30
+ max_index = np.argmax(predictions)
31
+ st.write(f"result (AutoModel) - Label: {max_index}")
32
 
 
 
 
 
33
 
34
+ if __name__ == "__main__":
35
+ main()
 
 
 
 
36
 
37
 
 
 
 
 
38
 
39
+ # import streamlit as st
40
+ # from transformers import pipeline
 
 
41
 
42
+ # # img2text
43
+ # def img2text(url):
44
+ # image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
45
+ # text = image_to_text_model(url)[0]["generated_text"]
46
 
47
+ # print(text)
48
+ # return text
 
 
 
 
49
 
50
+ # # txt2Story
51
+ # def txt2story(text):
52
+ # pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
53
+ # story_txt = pipe(text)[0]['generated_text']
54
 
55
+ # print(story_txt)
56
+ # return story_txt
57
+
58
+ # # Story2Audio
59
+ # def text2audio(story_text):
60
+ # pipe = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
61
+ # audio_data = pipe(story_text)
62
+ # return audio_data
63
+
64
+
65
+ # def main():
66
+ # st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
67
+ # st.header("Turn Your Image to Audio Story")
68
+ # uploaded_file = st.file_uploader("Select an Image...")
69
+
70
+ # if uploaded_file is not None:
71
+ # print(uploaded_file)
72
+ # bytes_data = uploaded_file.getvalue()
73
+ # with open(uploaded_file.name, "wb") as file:
74
+ # file.write(bytes_data)
75
+ # st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
76
+
77
+
78
+ # #Stage 1: Image to Text
79
+ # st.text('Processing img2text...')
80
+ # scenario = img2text(uploaded_file.name)
81
+ # st.write(scenario)
82
+
83
+ # #Stage 2: Text to Story
84
+ # st.text('Generating a story...')
85
+ # story = txt2story(scenario)
86
+ # st.write(story)
87
+
88
+ # #Stage 3: Story to Audio data
89
+ # st.text('Generating audio data...')
90
+ # audio_data =text2audio(story)
91
+
92
+ # # Play button
93
+ # if st.button("Play Audio"):
94
+ # st.audio(audio_data['audio'],
95
+ # format="audio/wav",
96
+ # start_time=0,
97
+ # sample_rate = audio_data['sampling_rate'])
98
+
99
+
100
+ # if __name__ == "__main__":
101
+ # main()