Delete 321Backup_app.py
Browse files- 321Backup_app.py +0 -70
321Backup_app.py
DELETED
@@ -1,70 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
3 |
-
import requests
|
4 |
-
from PIL import Image
|
5 |
-
import io
|
6 |
-
|
7 |
-
def load_image(input_type, uploaded_file=None, image_url=""):
|
8 |
-
"""
|
9 |
-
Loads an image from an uploaded file or URL.
|
10 |
-
"""
|
11 |
-
if input_type == "Upload Image" and uploaded_file is not None:
|
12 |
-
return Image.open(io.BytesIO(uploaded_file.getvalue()))
|
13 |
-
elif input_type == "Image URL" and image_url:
|
14 |
-
try:
|
15 |
-
response = requests.get(image_url)
|
16 |
-
return Image.open(io.BytesIO(response.content))
|
17 |
-
except Exception as e:
|
18 |
-
st.error(f"Error loading image from URL: {e}")
|
19 |
-
return None
|
20 |
-
|
21 |
-
def image_to_caption(image, input_type, uploaded_file, image_url):
|
22 |
-
"""
|
23 |
-
Generates a caption for the given image.
|
24 |
-
"""
|
25 |
-
image_to_text_pipeline = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
26 |
-
if input_type == "Upload Image" and uploaded_file:
|
27 |
-
return image_to_text_pipeline(uploaded_file.getvalue())[0]['generated_text']
|
28 |
-
elif input_type == "Image URL" and image_url:
|
29 |
-
return image_to_text_pipeline(image_url)[0]['generated_text']
|
30 |
-
return ""
|
31 |
-
|
32 |
-
def generate_text_from_caption(caption):
|
33 |
-
"""
|
34 |
-
Generates text based on the provided caption.
|
35 |
-
"""
|
36 |
-
model = AutoModelForCausalLM.from_pretrained('pranavpsv/genre-story-generator-v2')
|
37 |
-
tokenizer = AutoTokenizer.from_pretrained('pranavpsv/genre-story-generator-v2')
|
38 |
-
input_ids = tokenizer.encode(caption, return_tensors='pt')
|
39 |
-
generated_outputs = model.generate(input_ids, max_length=100, num_return_sequences=1)
|
40 |
-
return tokenizer.decode(generated_outputs[0], skip_special_tokens=True)
|
41 |
-
|
42 |
-
def main():
|
43 |
-
st.title('Image to Story Converter')
|
44 |
-
|
45 |
-
# User interface for input selection
|
46 |
-
input_type = st.radio("Select input type:", ("Upload Image", "Image URL"))
|
47 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) if input_type == "Upload Image" else None
|
48 |
-
image_url = st.text_input("Enter the image URL here:", "") if input_type == "Image URL" else ""
|
49 |
-
|
50 |
-
# Load image based on input selection
|
51 |
-
image = load_image(input_type, uploaded_file, image_url)
|
52 |
-
if image:
|
53 |
-
st.image(image, caption='Selected Image', use_column_width=True)
|
54 |
-
|
55 |
-
# Process image and generate text
|
56 |
-
if st.button('Generate Caption and Continue'):
|
57 |
-
if image:
|
58 |
-
with st.spinner("Processing..."):
|
59 |
-
# Convert image to text
|
60 |
-
caption = image_to_caption(image, input_type, uploaded_file, image_url)
|
61 |
-
st.success(f'Caption: {caption}')
|
62 |
-
|
63 |
-
# Generate additional text based on the caption
|
64 |
-
generated_text = generate_text_from_caption(caption)
|
65 |
-
st.text_area("Generated Story:", generated_text, height=200)
|
66 |
-
else:
|
67 |
-
st.error("Please upload an image or enter an image URL first.")
|
68 |
-
|
69 |
-
if __name__ == "__main__":
|
70 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|