Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Initialize Hugging Face Model
|
| 7 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 8 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 9 |
+
|
| 10 |
+
# Streamlit app layout
|
| 11 |
+
st.title("AI Multimodal File Processing App")
|
| 12 |
+
st.write("This app uses Hugging Face's CLIP model to analyze and edit image and text files.")
|
| 13 |
+
|
| 14 |
+
# Option to upload an image
|
| 15 |
+
st.subheader("Upload an Image")
|
| 16 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 17 |
+
|
| 18 |
+
if uploaded_image is not None:
|
| 19 |
+
image = Image.open(uploaded_image)
|
| 20 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 21 |
+
|
| 22 |
+
text = st.text_input("Enter text for image description", "A picture of a cat") # Default text
|
| 23 |
+
|
| 24 |
+
if st.button("Analyze Image"):
|
| 25 |
+
# Process the image with CLIP
|
| 26 |
+
inputs = processor(text=text, images=image, return_tensors="pt", padding=True)
|
| 27 |
+
outputs = model(**inputs)
|
| 28 |
+
|
| 29 |
+
# Display the similarity score between text and image
|
| 30 |
+
logits_per_image = outputs.logits_per_image # Similarity score
|
| 31 |
+
st.write(f"Similarity Score: {logits_per_image.item()}")
|
| 32 |
+
st.write(f"Description: {text}")
|
| 33 |
+
|
| 34 |
+
# Option to upload a text file
|
| 35 |
+
st.subheader("Upload a Text File")
|
| 36 |
+
uploaded_textfile = st.file_uploader("Choose a text file...", type=["txt"])
|
| 37 |
+
|
| 38 |
+
if uploaded_textfile is not None:
|
| 39 |
+
text_data = uploaded_textfile.read().decode('utf-8')
|
| 40 |
+
st.text_area("Text File Content", text_data, height=200)
|
| 41 |
+
|
| 42 |
+
if st.button("Process Text"):
|
| 43 |
+
# Example: Modify the text (e.g., convert to uppercase)
|
| 44 |
+
processed_text = text_data.upper()
|
| 45 |
+
st.write("Processed Text:")
|
| 46 |
+
st.text_area("Processed Text Output", processed_text, height=200)
|
| 47 |
+
|
| 48 |
+
# Optional: File download button
|
| 49 |
+
if uploaded_image is not None or uploaded_textfile is not None:
|
| 50 |
+
st.download_button("Download Processed File", data="Some file data here", file_name="processed_file.txt")
|
| 51 |
+
|