Spaces:
Paused
Paused
Commit
·
7a10318
1
Parent(s):
26bfbb3
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,69 @@
|
|
| 1 |
import asyncio
|
| 2 |
import websockets
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
async def
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
while True:
|
| 13 |
-
response = await
|
| 14 |
-
await
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import websockets
|
| 3 |
+
import subprocess
|
| 4 |
+
import os
|
| 5 |
+
import signal
|
| 6 |
|
| 7 |
+
async def take_input(websocket,prompt):
|
| 8 |
+
await websocket.send(f'input:{prompt}')
|
| 9 |
+
user_input = await websocket.recv()
|
| 10 |
+
with open('input.txt', 'w') as f:
|
| 11 |
+
f.write(user_input )
|
| 12 |
+
myinput = open('input.txt')
|
| 13 |
+
return user_input
|
| 14 |
+
|
| 15 |
+
async def run_code(websocket, path):
|
| 16 |
+
try:
|
| 17 |
+
# Read the code from the WebSocket connection
|
| 18 |
+
code = await websocket.recv()
|
| 19 |
+
# mod_code = """def make_input(*args):
|
| 20 |
+
# if args:
|
| 21 |
+
# print(f'>:{args[0]}')
|
| 22 |
+
# value = input()
|
| 23 |
+
# return value
|
| 24 |
+
# else:
|
| 25 |
+
# print(f'>:')
|
| 26 |
+
# value = input()
|
| 27 |
+
# return value"""+'\n'+code.replace("input(", "make_input(")
|
| 28 |
+
|
| 29 |
+
async with websockets.connect('wss://ramesh-vani-wspython.hf.space') as web:
|
| 30 |
+
await web.send(code)
|
| 31 |
while True:
|
| 32 |
+
response = await web.recv()
|
| 33 |
+
await send_message(response)
|
| 34 |
+
break
|
| 35 |
+
|
| 36 |
+
async def send_message(message):
|
| 37 |
+
await websocket.send(f'data: {message}')
|
| 38 |
+
|
| 39 |
+
# async def process_input_request(line):
|
| 40 |
+
# if line.strip().startswith(b">:"):
|
| 41 |
+
# # if b'input:' in line.strip():
|
| 42 |
+
# prompt = line.strip()[2:]
|
| 43 |
+
# user_input = await take_input(websocket,prompt)
|
| 44 |
+
# process.stdin.write(user_input.encode('utf-8') + b'\n')
|
| 45 |
+
# await process.stdin.drain()
|
| 46 |
+
|
| 47 |
+
# async for line in process.stdout:
|
| 48 |
+
# await process_input_request(line)
|
| 49 |
+
# await send_message(line.strip())
|
| 50 |
+
|
| 51 |
+
# async for line in process.stderr:
|
| 52 |
+
# print(f'error:{line.strip()}')
|
| 53 |
+
# await send_message(f'error:{line.strip()}')
|
| 54 |
+
|
| 55 |
+
# return_code = await process.wait()
|
| 56 |
+
# if return_code == 0:
|
| 57 |
+
# # await send_message('Code executed successfully')
|
| 58 |
+
# pass
|
| 59 |
+
# else:
|
| 60 |
+
# await send_message(f'error:Execution failed with return code {return_code}')
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f'error: {str(e)}')
|
| 64 |
+
await websocket.send(f'error: {str(e)}')
|
| 65 |
+
|
| 66 |
+
start_server = websockets.serve(run_code, "0.0.0.0", 7860)
|
| 67 |
+
|
| 68 |
+
asyncio.get_event_loop().run_until_complete(start_server)
|
| 69 |
+
asyncio.get_event_loop().run_forever()
|