MalikShehram commited on
Commit
00b6148
·
verified ·
1 Parent(s): 9416b7f

Update backend/main.py

Browse files
Files changed (1) hide show
  1. backend/main.py +24 -32
backend/main.py CHANGED
@@ -1,43 +1,35 @@
1
  from fastapi import FastAPI
2
  from fastapi.staticfiles import StaticFiles
3
  from pydantic import BaseModel
4
- import random
 
 
5
  from backend.comm_system import simulate_channel
6
  from backend.ai_decoder import decode_semantic_intent
7
 
8
  app = FastAPI()
9
 
10
- class ChannelRequest(BaseModel):
11
- snr_db: float
12
- message: str
13
-
14
- @app.post("/api/simulate")
15
- def simulate_transmission(req: ChannelRequest):
16
- # 1. Run the physics simulation for the UI diagram
17
- constellation_data = simulate_channel(req.snr_db)
18
-
19
- # 2. Simulate traditional Bit Error Rate (BER) text corruption
20
- # Lower SNR = More errors in the string
21
- corrupted = list(req.message)
22
- error_rate = max(0, (15 - req.snr_db) / 30)
23
-
24
- for i in range(len(corrupted)):
25
- if random.random() < error_rate and corrupted[i] != " ":
26
- corrupted[i] = random.choice("!@#$%^&*~?")
27
-
28
- corrupted_message = "".join(corrupted)
29
-
30
- # 3. Trigger Generative AI to fix the signal if corruption occurred
31
- if error_rate > 0:
32
- recovered_message = decode_semantic_intent(corrupted_message)
33
- else:
34
- recovered_message = req.message
35
 
36
- return {
37
- "constellation": constellation_data,
38
- "corrupted_message": corrupted_message,
39
- "recovered_message": recovered_message
40
- }
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- # Mount the React App (Must be at the bottom so it doesn't block /api)
43
  app.mount("/", StaticFiles(directory="frontend/dist", html=True), name="static")
 
1
  from fastapi import FastAPI
2
  from fastapi.staticfiles import StaticFiles
3
  from pydantic import BaseModel
4
+ import os
5
+
6
+ # Import your simulation and AI scripts
7
  from backend.comm_system import simulate_channel
8
  from backend.ai_decoder import decode_semantic_intent
9
 
10
  app = FastAPI()
11
 
12
+ # This tells Python exactly what the React app is sending
13
+ class MessageRequest(BaseModel):
14
+ text: str
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # 📡 The endpoint that listens for the "Transmit Message" button
17
+ @app.post("/api/transmit")
18
+ async def transmit_message(req: MessageRequest):
19
+ try:
20
+ # 1. Pass the user's text through the noisy channel
21
+ corrupted_text = simulate_channel(req.text)
22
+
23
+ # 2. Ask the Hugging Face AI to fix it
24
+ decoded_text = decode_semantic_intent(corrupted_text)
25
+
26
+ # 3. Send both back to the React UI
27
+ return {
28
+ "corrupted": corrupted_text,
29
+ "decoded": decoded_text
30
+ }
31
+ except Exception as e:
32
+ return {"corrupted": "Error in channel", "decoded": f"Backend Error: {str(e)}"}
33
 
34
+ # Mount the React frontend so the browser can see it
35
  app.mount("/", StaticFiles(directory="frontend/dist", html=True), name="static")