Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,21 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
-
import torch
|
| 4 |
-
import requests
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from io import BytesIO
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return_tensors="pt",
|
| 21 |
-
truncation=True, # Ensure text is truncated to fit model input size
|
| 22 |
-
padding=True # Pad shorter sequences so that all are the same length
|
| 23 |
-
)['input_ids']
|
| 24 |
-
|
| 25 |
-
text_features = model.get_text_features(processed_texts)
|
| 26 |
-
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
| 27 |
-
|
| 28 |
-
# Prediction function
|
| 29 |
-
def predict_from_url(url):
|
| 30 |
-
# Check if the URL is empty
|
| 31 |
-
if not url:
|
| 32 |
-
return {"Error": "Please input a URL"}
|
| 33 |
-
|
| 34 |
-
try:
|
| 35 |
-
image = Image.open(BytesIO(requests.get(url).content))
|
| 36 |
-
except Exception as e:
|
| 37 |
-
return {"Error": f"Failed to load image: {str(e)}"}
|
| 38 |
-
|
| 39 |
-
processed_image = processor(images=image, return_tensors="pt")['pixel_values']
|
| 40 |
-
|
| 41 |
-
with torch.no_grad():
|
| 42 |
-
image_features = model.get_image_features(processed_image)
|
| 43 |
-
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
| 44 |
-
text_probs = (100 * image_features @ text_features.T).softmax(dim=-1)
|
| 45 |
-
|
| 46 |
-
return {fashion_items[i]: float(text_probs[0, i]) for i in range(len(fashion_items))}
|
| 47 |
-
|
| 48 |
-
# Gradio interface
|
| 49 |
-
demo = gr.Interface(
|
| 50 |
-
fn=predict_from_url,
|
| 51 |
-
inputs=gr.Textbox(label="Enter Image URL"),
|
| 52 |
-
outputs=gr.Label(label="Classification Results"),
|
| 53 |
-
title="Fashion Item Classifier",
|
| 54 |
-
allow_flagging="never"
|
| 55 |
)
|
| 56 |
|
| 57 |
# Launch the interface
|
| 58 |
-
|
|
|
|
| 1 |
+
# Import libraries
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Create a summarization pipeline
|
| 6 |
+
summarizer = pipeline("summarization")
|
| 7 |
|
| 8 |
+
# Define a function that takes a text and returns a summary
|
| 9 |
+
def summarize(text):
|
| 10 |
+
summary = summarizer(text, max_length=150, min_length=40, do_sample=False)[0]
|
| 11 |
+
return summary["summary_text"]
|
| 12 |
|
| 13 |
+
# Create a Gradio interface
|
| 14 |
+
interface = gr.Interface(
|
| 15 |
+
fn=summarize, # the function to wrap
|
| 16 |
+
inputs=gr.Textbox(lines=10, label="Input Text"), # the input component
|
| 17 |
+
outputs=gr.Textbox(label="Summary") # the output component
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
# Launch the interface
|
| 21 |
+
interface.launch()
|