AidenYan commited on
Commit
34571c8
·
verified ·
1 Parent(s): 6b4b227

Create app_Jim20240322.py

Browse files
Files changed (1) hide show
  1. app_Jim20240322.py +78 -0
app_Jim20240322.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ #from diffusers import DiffusionPipeline
4
+ from PIL import Image
5
+ import requests
6
+ import io
7
+ from io import BytesIO
8
+
9
+ # Load the image-to-text pipeline
10
+ image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
11
+
12
+ # Load the text mask pipeline
13
+ generate_mask = pipeline("fill-mask", model="google-bert/bert-base-uncased")
14
+
15
+ # Load the text generation pipeline
16
+ extend_text = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
17
+
18
+ # Load the text-to-image model
19
+ #text_to_image = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
20
+
21
+ def main():
22
+ st.title("SmartCart (Product Recommender)")
23
+
24
+ # User input for text or URL
25
+ input_option = st.radio("Select input option:", ("Text", "URL"))
26
+
27
+ # Input text
28
+ if input_option == "Text":
29
+ text_input = st.text_input("Enter the text:")
30
+ if st.button("Generate Story and Image") and text_input:
31
+ #generate_image(text_input)
32
+ generated_text = generate_mask_from_result(text_input)
33
+ st.success(f'Generated Caption: {text_input}')
34
+ st.success(f'Generated Text: {generated_text}')
35
+
36
+
37
+ # Input URL
38
+ elif input_option == "URL":
39
+ image_url = st.text_input("Enter the image URL:")
40
+ if st.button("Generate Story and Image") and image_url:
41
+ image_text = image_to_text_from_url(image_url)
42
+ #generate_image(image_text)
43
+ generated_text = generate_mask_from_result(image_text)
44
+ st.success(f'Generated Caption: {image_text}')
45
+ st.success(f'Generated Text: {generated_text}')
46
+
47
+
48
+ def image_to_text_from_file(uploaded_file):
49
+ image_bytes = io.BytesIO(uploaded_file.read())
50
+ return image_to_text(image_bytes)[0]['generated_text']
51
+
52
+ def image_to_text_from_url(image_url):
53
+ response = requests.get(image_url)
54
+ image_bytes = Image.open(BytesIO(response.content))
55
+ return image_to_text(image_bytes)[0]['generated_text']
56
+
57
+ def generate_image(text):
58
+ rephrased_text = "I want to buy " + text + " and [MASK] for my children"
59
+ generated_image = text_to_image(rephrased_text)
60
+ st.image(generated_image, caption="Generated Image", use_column_width=True)
61
+
62
+ def generate_mask_from_result(text):
63
+ output = generate_mask(f"I want to buy 2 toys for my children. I will buy {text} and [MASK].")
64
+
65
+ if output and output[0]['token_str'] == text:
66
+ # If the first result matches the input, get the second output instead
67
+ second_output = output[1] if len(output) > 1 else None
68
+ result = second_output['token_str'] if second_output else None
69
+ else:
70
+ result = output[0]['token_str'] if output else None
71
+
72
+ extended_text = extend_text(f"A child with {text} and {result} ")
73
+ return extended_text[0]['generated_text']
74
+
75
+
76
+ if __name__ == "__main__":
77
+ main()
78
+