Spaces:
Sleeping
Sleeping
Create modules/app.py
Browse files- modules/app.py +140 -0
modules/app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Form
|
2 |
+
from fastapi.responses import HTMLResponse, StreamingResponse
|
3 |
+
import asyncio
|
4 |
+
import random
|
5 |
+
import string
|
6 |
+
import uvicorn
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
@app.get("/", response_class=HTMLResponse)
|
11 |
+
async def get_form():
|
12 |
+
return """
|
13 |
+
<html>
|
14 |
+
<head>
|
15 |
+
<title>URL Input</title>
|
16 |
+
<style>
|
17 |
+
body {
|
18 |
+
font-family: Arial, sans-serif;
|
19 |
+
display: flex;
|
20 |
+
justify-content: center;
|
21 |
+
align-items: center;
|
22 |
+
height: 100vh;
|
23 |
+
background-color: #f0f0f0;
|
24 |
+
}
|
25 |
+
form {
|
26 |
+
background: white;
|
27 |
+
padding: 2em;
|
28 |
+
border-radius: 5px;
|
29 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
30 |
+
margin-bottom: 20px;
|
31 |
+
}
|
32 |
+
input[type="text"] {
|
33 |
+
width: 300px;
|
34 |
+
padding: 0.5em;
|
35 |
+
margin-bottom: 1em;
|
36 |
+
border: 1px solid #ccc;
|
37 |
+
border-radius: 4px;
|
38 |
+
}
|
39 |
+
input[type="text"][name="filename"] {
|
40 |
+
width: 200px;
|
41 |
+
margin-left: 10px;
|
42 |
+
}
|
43 |
+
button {
|
44 |
+
padding: 0.5em 1em;
|
45 |
+
color: white;
|
46 |
+
background-color: #007BFF;
|
47 |
+
border: none;
|
48 |
+
border-radius: 4px;
|
49 |
+
cursor: pointer;
|
50 |
+
}
|
51 |
+
button:hover {
|
52 |
+
background-color: #0056b3;
|
53 |
+
}
|
54 |
+
.output-box {
|
55 |
+
width: 80%;
|
56 |
+
height: 400px;
|
57 |
+
border: 1px solid #ccc;
|
58 |
+
border-radius: 4px;
|
59 |
+
overflow-y: auto;
|
60 |
+
padding: 1em;
|
61 |
+
background-color: white;
|
62 |
+
}
|
63 |
+
.output-content {
|
64 |
+
white-space: pre-wrap;
|
65 |
+
font-family: monospace;
|
66 |
+
}
|
67 |
+
</style>
|
68 |
+
</head>
|
69 |
+
<body>
|
70 |
+
<div>
|
71 |
+
<form id="urlForm" action="/submit" method="post">
|
72 |
+
<input type="text" name="url" id="urlInput" placeholder="Enter URL" required>
|
73 |
+
<input type="text" name="filename" id="filenameInput" placeholder="File Name">
|
74 |
+
<button type="submit">Submit</button>
|
75 |
+
</form>
|
76 |
+
<div class="output-box" id="output-box">
|
77 |
+
<div class="output-content" id="output-content"></div>
|
78 |
+
</div>
|
79 |
+
</div>
|
80 |
+
<script>
|
81 |
+
document.getElementById('urlInput').addEventListener('input', function() {
|
82 |
+
const url = this.value;
|
83 |
+
const defaultFilename = url.split('/').pop() || '';
|
84 |
+
if (filenameInput.value !s== defaultFilename) {
|
85 |
+
filenameInput.value = defaultFilename;
|
86 |
+
}
|
87 |
+
});
|
88 |
+
|
89 |
+
const form = document.getElementById('urlForm');
|
90 |
+
form.addEventListener('submit', async function(event) {
|
91 |
+
event.preventDefault();
|
92 |
+
const formData = new FormData(form);
|
93 |
+
const response = await fetch('/submit', {
|
94 |
+
method: 'POST',
|
95 |
+
body: formData
|
96 |
+
});
|
97 |
+
|
98 |
+
const reader = response.body.getReader();
|
99 |
+
const decoder = new TextDecoder();
|
100 |
+
const outputContent = document.getElementById('output-content');
|
101 |
+
const outputBox = document.getElementById('output-box');
|
102 |
+
|
103 |
+
outputContent.innerHTML = ''; // Clear previous content
|
104 |
+
|
105 |
+
while (true) {
|
106 |
+
const { done, value } = await reader.read();
|
107 |
+
if (done) break;
|
108 |
+
const chunk = decoder.decode(value, { stream: true });
|
109 |
+
outputContent.innerHTML += chunk;
|
110 |
+
outputBox.scrollTop = outputBox.scrollHeight; // Scroll to the latest content
|
111 |
+
}
|
112 |
+
});
|
113 |
+
</script>
|
114 |
+
</body>
|
115 |
+
</html>
|
116 |
+
"""
|
117 |
+
|
118 |
+
@app.post("/submit")
|
119 |
+
async def submit_url(url: str = Form(...), filename: str = Form(None)):
|
120 |
+
if not filename:
|
121 |
+
filename = url.split("/")[-1]
|
122 |
+
|
123 |
+
async def generate_html():
|
124 |
+
yield f'<p>Filename: {filename}</p><p>Lorem Ipsum Text:</p>'
|
125 |
+
lorem_text = ''.join(random.choices(string.ascii_letters + string.digits + ' ', k=1000))
|
126 |
+
for char in lorem_text:
|
127 |
+
await asyncio.sleep(0.01) # 10ms delay
|
128 |
+
yield char
|
129 |
+
|
130 |
+
headers = {
|
131 |
+
"Content-Type": "text/html",
|
132 |
+
"Transfer-Encoding": "chunked",
|
133 |
+
"Cache-Control": "no-cache",
|
134 |
+
"X-Accel-Buffering": "no"
|
135 |
+
}
|
136 |
+
|
137 |
+
return StreamingResponse(generate_html(), headers=headers)
|
138 |
+
|
139 |
+
if __name__ == "__main__":
|
140 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|