Skip to content

Commit dda5033

Browse files
committed
removed comments in final render and added _FILES variable
1 parent 967c0bb commit dda5033

File tree

9 files changed

+616
-4
lines changed

9 files changed

+616
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ wheels/
88

99
# Virtual environments
1010
.venv
11+
12+
models/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* ✅ Built with Flask under the hood
1212
* ✅ Minimal setup — no frontend frameworks or build tools needed
1313
* ✅ Ideal for quick demos, server-rendered sites, teaching, and prototyping AI apps
14-
* ✅ Supports special variables like `_GET`, `_POST`, `GLOBAL`, and `SESSION` inside `<wep>` blocks for handling requests and session data
14+
* ✅ Supports special variables like `_GET`, `_POST`, `_FILES`, `GLOBAL`, and `SESSION` inside `<wep>` blocks for handling requests and session data
1515

1616

1717
---

load.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from transformers import BlipProcessor, BlipForConditionalGeneration
2+
3+
# to save model locally for Image Caption Generator
4+
5+
model = "Salesforce/blip-image-captioning-base"
6+
save = "./models/blip"
7+
8+
processor = BlipProcessor.from_pretrained(model)
9+
model = BlipForConditionalGeneration.from_pretrained(model)
10+
11+
processor.save_pretrained(save)
12+
model.save_pretrained(save)

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def echo(tag):
2424
"echo" : echo,
2525
"_GET" : request.args.to_dict(),
2626
"_POST" : request.form.to_dict(),
27+
"_FILES": request.files.to_dict(),
2728
"GLOBAL" : GLOBAL,
2829
"SESSION" : session
2930
}
@@ -36,9 +37,10 @@ def echo(tag):
3637
return f"<pre style='background-color:#7f1d1d; color:#fca5a5; padding:1rem; border-radius:0.5rem;'>Execution Error:\n{error}</pre>"
3738
try:
3839
processed_html = re.sub(r"<wep>(.*?)</wep>", replace_wep, html, flags=re.DOTALL)
40+
comments_removed = re.sub("(<!--.*?-->)","",processed_html,flags=re.DOTALL)
3941
except re.error as re_err:
4042
return Response(f"Regex Error: {re_err}", status=500, mimetype="text/plain")
41-
return Response(processed_html, mimetype="text/html; charset=utf-8")
43+
return Response(comments_removed, mimetype="text/html; charset=utf-8")
4244

4345
app = Flask(__name__)
4446
app.secret_key = 'web-embedded-python'

public/caption.wep

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Image Caption Generator Demo — WEP</title>
6+
<script src="https://cdn.tailwindcss.com"></script>
7+
<script>
8+
tailwind.config = {
9+
darkMode: 'class'
10+
}
11+
</script>
12+
</head>
13+
<body class="dark bg-gray-900 text-gray-100 min-h-screen flex flex-col items-center justify-start py-10 px-4">
14+
15+
<div class="w-full max-w-xl bg-gray-800 shadow-lg rounded-lg p-6">
16+
<h1 class="text-2xl font-bold mb-6 text-center text-teal-400">🖼️ Image Caption Generator</h1>
17+
18+
<form method="POST" enctype="multipart/form-data" class="flex flex-col items-center gap-4">
19+
<input type="file" name="myfile" accept="image/*" required
20+
class="block w-full text-sm text-gray-300
21+
file:mr-4 file:py-2 file:px-4
22+
file:rounded-md file:border-0
23+
file:text-sm file:font-semibold
24+
file:bg-teal-100 file:text-teal-900
25+
hover:file:bg-teal-200" />
26+
<button type="submit"
27+
class="bg-teal-500 text-white px-6 py-2 rounded-md hover:bg-teal-600 transition">
28+
Generate Caption
29+
</button>
30+
</form>
31+
32+
<div class="mt-8">
33+
<wep>
34+
import io
35+
import base64
36+
from PIL import Image
37+
from transformers import BlipProcessor, BlipForConditionalGeneration
38+
39+
processor = BlipProcessor.from_pretrained("./models/blip")
40+
model = BlipForConditionalGeneration.from_pretrained("./models/blip")
41+
42+
if "myfile" in _FILES:
43+
file = _FILES["myfile"]
44+
try:
45+
img_bytes = file.read()
46+
image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
47+
48+
inputs = processor(images=image, return_tensors="pt")
49+
output = model.generate(**inputs)
50+
caption = processor.decode(output[0], skip_special_tokens=True)
51+
52+
b64img = base64.b64encode(img_bytes).decode('utf-8')
53+
54+
echo(f'''
55+
<div class="text-center">
56+
<h3 class="text-lg font-semibold text-gray-200 mb-2">Caption:</h3>
57+
<p class="text-xl text-green-300 mb-4">{caption}</p>
58+
<img src="data:image/jpeg;base64,{b64img}" class="mx-auto rounded-lg shadow-lg max-w-xs border border-gray-600" />
59+
</div>
60+
''')
61+
except Exception as e:
62+
echo(f'<div class="text-red-400"><h3 class="font-bold">Something went wrong</h3><pre>{e}</pre></div>')
63+
else:
64+
echo("<p class='text-center text-gray-400'>No file uploaded yet.</p>")
65+
</wep>
66+
</div>
67+
</div>
68+
<footer class="pt-8 text-center text-gray-500 text-sm">
69+
Built with ❤️ using WEP — Web Embedded Python
70+
</footer>
71+
</body>
72+
</html>

public/index.wep

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,14 @@ else:
182182
</div>
183183
</section>
184184
<!-- Section 7: Additional Link -->
185-
<a href="/aidemo.wep">
186-
<h2 class="text-xl font-semibold text-teal-300 hover:underline">AI app demo</h2>
185+
<section>
186+
<a href="/lrdemo.wep">
187+
<h2 class="text-xl font-semibold text-teal-300 hover:underline">Linear Regression Demo</h2>
188+
</a>
189+
<a href="/caption.wep">
190+
<h2 class="text-xl font-semibold text-teal-300 hover:underline">Image Caption Generator Demo</h2>
187191
</a>
192+
</section>
188193
<!-- Footer -->
189194
<footer class="text-center text-gray-600 pt-12">
190195
<hr class="mb-4 border-gray-700" />
File renamed without changes.

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ dependencies = [
88
"flask>=3.1.1",
99
"matplotlib>=3.10.3",
1010
"numpy>=2.2.6",
11+
"pillow>=11.2.1",
12+
"torch>=2.7.1",
13+
"transformers>=4.53.0",
1114
]

0 commit comments

Comments
 (0)