Download app.py from subh71/myspacesk: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/spaces/subh71/myspacesk/resolve/main/app.py
- Command line
-
hf download hf://spaces/subh71/myspacesk/app.py
-
curl -L -o app.py https://huggingface.co/spaces/subh71/myspacesk/resolve/main/app.py
1.77 kB
| from transformers import pipeline | |
| import streamlit as st | |
| import torch | |
| from PIL import Image | |
| import requests | |
| # Load model directly | |
| from transformers import AutoImageProcessor, AutoModelForObjectDetection | |
| from transformers import AutoImageProcessor, DetrForObjectDetection | |
| def model(): | |
| url = "http://images.cocodataset.org/val2017/000000039769.jpg" | |
| image = Image.open(requests.get(url, stream=True).raw) | |
| image_processor = AutoImageProcessor.from_pretrained("facebook/detr-resnet-50") | |
| model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") | |
| inputs = image_processor(images=image, return_tensors="pt") | |
| outputs = model(**inputs) | |
| # display image | |
| st.image(image, caption='Animal Images') | |
| # convert outputs (bounding boxes and class logits) to Pascal VOC format (xmin, ymin, xmax, ymax) | |
| target_sizes = torch.tensor([image.size[::-1]]) | |
| results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0] | |
| for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): | |
| box = [round(i, 2) for i in box.tolist()] | |
| st.write(f"Detected {model.config.id2label[label.item()]} with confidence ") | |
| st.write(f"{round(score.item(), 3)} at location {box}") | |
| def main(): | |
| st.title("Hugging Face Model Demo") | |
| # Create an input text box | |
| input_text = st.text_input("Enter your text", "") | |
| # Create a button to trigger model inference | |
| if st.button("Analyze"): | |
| # Perform inference using the loaded model | |
| result=model() | |
| #st.write("Prediction:", result[0]['label'], "| Score:", result[0]['score']) | |
| if __name__ == "__main__": | |
| main() | |