Spaces:
Sleeping
Sleeping
VenkateshRoshan commited on
Commit ·
8eeb5a3
1
Parent(s): bf9aafc
Added Webpage
Browse files- app.py +3 -2
- static/styles.css +18 -0
- templates/index.html +148 -0
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
| 4 |
from infer import ImageCaptioningInference
|
|
@@ -23,7 +23,8 @@ inference_model = ImageCaptioningInference(model)
|
|
| 23 |
|
| 24 |
@app.route('/')
|
| 25 |
def home():
|
| 26 |
-
return "Welcome to the Flask API"
|
|
|
|
| 27 |
|
| 28 |
@app.route('/upload-image', methods=['POST'])
|
| 29 |
def upload_image():
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
| 4 |
from infer import ImageCaptioningInference
|
|
|
|
| 23 |
|
| 24 |
@app.route('/')
|
| 25 |
def home():
|
| 26 |
+
# return "Welcome to the Flask API"
|
| 27 |
+
return render_template('index.html')
|
| 28 |
|
| 29 |
@app.route('/upload-image', methods=['POST'])
|
| 30 |
def upload_image():
|
static/styles.css
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
body {
|
| 2 |
+
font-family: Arial, sans-serif;
|
| 3 |
+
margin: 0;
|
| 4 |
+
padding: 20px;
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
h1 {
|
| 8 |
+
color: #333;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
form {
|
| 12 |
+
margin-bottom: 20px;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
input[type="file"], input[type="text"] {
|
| 16 |
+
display: block;
|
| 17 |
+
margin: 10px 0;
|
| 18 |
+
}
|
templates/index.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ial-scale=1.0">
|
| 6 |
+
<title>Image Inference</title>
|
| 7 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
|
| 8 |
+
<style>
|
| 9 |
+
/* Drag and drop area styles */
|
| 10 |
+
#drop-area {
|
| 11 |
+
border: 2px dashed #ccc;
|
| 12 |
+
border-radius: 20px;
|
| 13 |
+
width: 300px;
|
| 14 |
+
height: 300px;
|
| 15 |
+
margin: 20px auto;
|
| 16 |
+
text-align: center;
|
| 17 |
+
padding: 20px;
|
| 18 |
+
transition: border 0.3s;
|
| 19 |
+
position: relative;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
#drop-area.highlight {
|
| 23 |
+
border-color: #666;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
#drop-area p {
|
| 27 |
+
margin: 0;
|
| 28 |
+
font-weight: bold;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
#drop-area img {
|
| 32 |
+
max-width: 100%;
|
| 33 |
+
max-height: 100%;
|
| 34 |
+
display: none;
|
| 35 |
+
position: absolute;
|
| 36 |
+
top: 0;
|
| 37 |
+
left: 0;
|
| 38 |
+
right: 0;
|
| 39 |
+
bottom: 0;
|
| 40 |
+
margin: auto;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
#caption-box {
|
| 44 |
+
margin-top: 20px;
|
| 45 |
+
width: 300px;
|
| 46 |
+
height: 100px;
|
| 47 |
+
resize: none;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
#caption-container {
|
| 51 |
+
text-align: center;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
#upload-container {
|
| 55 |
+
display: flex;
|
| 56 |
+
justify-content: center;
|
| 57 |
+
align-items: center;
|
| 58 |
+
flex-direction: column;
|
| 59 |
+
margin-top: 20px;
|
| 60 |
+
}
|
| 61 |
+
</style>
|
| 62 |
+
</head>
|
| 63 |
+
<body>
|
| 64 |
+
<h1>Image Inference App</h1>
|
| 65 |
+
<div id="upload-container">
|
| 66 |
+
<div id="drop-area">
|
| 67 |
+
<p>Drag & drop your image here or click to select</p>
|
| 68 |
+
<input type="file" name="image" accept="image/*" required style="display:none;">
|
| 69 |
+
<img id="uploaded-image" src="" alt="Uploaded Image"> <!-- Image will appear here -->
|
| 70 |
+
</div>
|
| 71 |
+
</div>
|
| 72 |
+
|
| 73 |
+
<div id="caption-container">
|
| 74 |
+
<h2>Generated Caption:</h2>
|
| 75 |
+
<textarea id="caption-box" readonly></textarea> <!-- This will display the caption -->
|
| 76 |
+
</div>
|
| 77 |
+
|
| 78 |
+
<script>
|
| 79 |
+
const dropArea = document.getElementById('drop-area');
|
| 80 |
+
const input = dropArea.querySelector('input[type="file"]');
|
| 81 |
+
const uploadedImage = document.getElementById('uploaded-image');
|
| 82 |
+
const captionBox = document.getElementById('caption-box');
|
| 83 |
+
|
| 84 |
+
// Prevent default drag behaviors
|
| 85 |
+
;['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
| 86 |
+
dropArea.addEventListener(eventName, preventDefaults, false);
|
| 87 |
+
document.body.addEventListener(eventName, preventDefaults, false);
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
// Highlight the drop area when an item is dragged over it
|
| 91 |
+
;['dragenter', 'dragover'].forEach(eventName => {
|
| 92 |
+
dropArea.classList.add('highlight');
|
| 93 |
+
});
|
| 94 |
+
|
| 95 |
+
;['dragleave', 'drop'].forEach(eventName => {
|
| 96 |
+
dropArea.classList.remove('highlight');
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
// Handle dropped files
|
| 100 |
+
dropArea.addEventListener('drop', handleDrop, false);
|
| 101 |
+
dropArea.addEventListener('click', () => input.click());
|
| 102 |
+
|
| 103 |
+
function preventDefaults(e) {
|
| 104 |
+
e.preventDefault();
|
| 105 |
+
e.stopPropagation();
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
// Handle file input change
|
| 109 |
+
input.addEventListener('change', () => {
|
| 110 |
+
handleFiles(input.files);
|
| 111 |
+
});
|
| 112 |
+
|
| 113 |
+
function handleDrop(e) {
|
| 114 |
+
const dt = e.dataTransfer;
|
| 115 |
+
const files = dt.files;
|
| 116 |
+
handleFiles(files);
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
function handleFiles(files) {
|
| 120 |
+
if (files.length > 0) {
|
| 121 |
+
// Clear previous outputs
|
| 122 |
+
captionBox.value = '';
|
| 123 |
+
uploadedImage.style.display = 'none';
|
| 124 |
+
|
| 125 |
+
const formData = new FormData();
|
| 126 |
+
formData.append('image', files[0]);
|
| 127 |
+
|
| 128 |
+
// Show the uploaded image in the container
|
| 129 |
+
uploadedImage.src = URL.createObjectURL(files[0]);
|
| 130 |
+
uploadedImage.style.display = 'block'; // Make the image visible
|
| 131 |
+
|
| 132 |
+
// Perform AJAX request to upload image without refreshing the page
|
| 133 |
+
fetch("/upload-image", {
|
| 134 |
+
method: "POST",
|
| 135 |
+
body: formData,
|
| 136 |
+
})
|
| 137 |
+
.then(response => response.json())
|
| 138 |
+
.then(result => {
|
| 139 |
+
captionBox.value = result.generated_caption || result.error; // Display caption in the text area
|
| 140 |
+
})
|
| 141 |
+
.catch(error => {
|
| 142 |
+
console.error('Error:', error);
|
| 143 |
+
});
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
</script>
|
| 147 |
+
</body>
|
| 148 |
+
</html>
|