MalikShehram's picture
Update backend/main.py
00b6148 verified
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
import os
# Import your simulation and AI scripts
from backend.comm_system import simulate_channel
from backend.ai_decoder import decode_semantic_intent
app = FastAPI()
# This tells Python exactly what the React app is sending
class MessageRequest(BaseModel):
text: str
# 📡 The endpoint that listens for the "Transmit Message" button
@app.post("/api/transmit")
async def transmit_message(req: MessageRequest):
try:
# 1. Pass the user's text through the noisy channel
corrupted_text = simulate_channel(req.text)
# 2. Ask the Hugging Face AI to fix it
decoded_text = decode_semantic_intent(corrupted_text)
# 3. Send both back to the React UI
return {
"corrupted": corrupted_text,
"decoded": decoded_text
}
except Exception as e:
return {"corrupted": "Error in channel", "decoded": f"Backend Error: {str(e)}"}
# Mount the React frontend so the browser can see it
app.mount("/", StaticFiles(directory="frontend/dist", html=True), name="static")