Ramesh-vani commited on
Commit
7a10318
1 Parent(s): 26bfbb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -28
app.py CHANGED
@@ -1,32 +1,69 @@
1
  import asyncio
2
  import websockets
 
 
 
3
 
4
- async def handle_user_client(websocket, path):
5
- code = await websocket.recv()
6
- external_uri = 'wss://ramesh-vani-wspython.hf.space'
7
-
8
- async with websockets.connect(external_uri) as external_ws:
9
- async def forward_user_to_external():
10
-
11
- await external_ws.send(code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  while True:
13
- response = await external_ws.recv()
14
- await websocket.send(f'{response}')
15
-
16
- async def forward_external_to_user():
17
- async for external_message in external_ws:
18
- print(f"Received from external server: {external_message}")
19
- await websocket.send(external_message)
20
-
21
- # Start two tasks to handle message forwarding in both directions
22
- await asyncio.gather(
23
- forward_user_to_external(),
24
- # forward_external_to_user(),
25
- )
26
-
27
- # Set up WebSocket server for user
28
- user_server = websockets.serve(handle_user_client, "0.0.0.0", 7860)
29
-
30
- # Start the server and run the event loop
31
- asyncio.get_event_loop().run_until_complete(user_server)
32
- asyncio.get_event_loop().run_forever()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()