Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,37 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
tokenizer = BartTokenizer.from_pretrained(model_name)
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
# Input
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
if input_text:
|
22 |
-
# Tokenize and summarize the input text
|
23 |
-
inputs = tokenizer(input_text, return_tensors="pt", max_length=1024, truncation=True)
|
24 |
-
summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
|
25 |
-
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
26 |
-
st.subheader("Summary:")
|
27 |
-
st.write(summary)
|
28 |
|
29 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import CLIPProcessor, CLIPModel
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
|
6 |
+
# Load the pre-trained CLIP model and processor
|
7 |
+
model_name = "facebook/nougat-base" needed
|
8 |
+
model = CLIPModel.from_pretrained(model_name)
|
9 |
+
processor = CLIPProcessor.from_pretrained(model_name)
|
10 |
|
11 |
+
st.title("Image to Text Conversion App")
|
12 |
+
|
13 |
+
# Input image upload
|
14 |
+
image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
15 |
|
16 |
+
if image:
|
17 |
+
# Display the uploaded image
|
18 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
|
|
19 |
|
20 |
+
# Process the image for text conversion
|
21 |
+
with torch.no_grad():
|
22 |
+
inputs = processor(text="a photo of " + st.session_state["alt_text"], images=image, return_tensors="pt")
|
23 |
+
outputs = model(**inputs)
|
24 |
+
|
25 |
+
# Extract the textual description
|
26 |
+
text_description = processor.decode(outputs["text"])
|
27 |
+
|
28 |
+
# Display the text description
|
29 |
+
st.subheader("Text Description:")
|
30 |
+
st.write(text_description)
|
31 |
|
32 |
+
# Input for alternative text
|
33 |
+
alt_text = st.text_area("Provide alternative text for the image:", key="alt_text")
|
34 |
|
35 |
+
st.write("Powered by Hugging Face's CLIP model.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
streamlit run app.py
|