Skip to content

Commit db0520c

Browse files
committed
added parrallel processing to the frontend with Promise and to the backend with asyncio
1 parent 53a1c3b commit db0520c

3 files changed

Lines changed: 33 additions & 53 deletions

File tree

backend/app/routes/routes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
from pydantic import BaseModel
33
from app.modules.pipeline import run_scraper_pipeline
44
from app.modules.pipeline import run_langgraph_workflow
5-
import json
65
from app.modules.bias_detection.check_bias import check_bias
6+
import asyncio
7+
import json
78

89
router = APIRouter()
910

10-
1111
class URlRequest(BaseModel):
1212
url: str
1313

@@ -18,16 +18,16 @@ async def home():
1818

1919
@router.post("/bias")
2020
async def bias_detection(request: URlRequest):
21-
content = run_scraper_pipeline(request.url)
22-
bias_score = check_bias(content)
21+
content = await asyncio.to_thread(run_scraper_pipeline,(request.url))
22+
bias_score = await asyncio.to_thread(check_bias,(content))
2323
print(bias_score)
2424
return bias_score
2525

2626

2727

2828
@router.post("/process")
2929
async def run_pipelines(request: URlRequest):
30-
article_text = run_scraper_pipeline(request.url)
30+
article_text = await asyncio.to_thread(run_scraper_pipeline,(request.url))
3131
print(json.dumps(article_text, indent=2))
32-
data = run_langgraph_workflow(article_text)
32+
data = await asyncio.to_thread(run_langgraph_workflow,(article_text))
3333
return data

frontend/app/analyze/loading/page.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,19 @@ export default function LoadingPage() {
7777
}
7878
),
7979
axios.post(
80-
"https://Thunder1245-perspective-backend.hf.space/api/bias",
80+
"http://Thunder1245-perspective-backend.hf.space/api/bias",
8181
{
8282
url: storedUrl,
8383
}
8484
),
8585
]);
8686

87+
88+
sessionStorage.setItem("BiasScore", JSON.stringify(biasRes.data));
89+
90+
console.log("Bias score saved");
91+
console.log(biasRes);
92+
8793
// Save response to sessionStorage
8894
sessionStorage.setItem(
8995
"analysisResult",
@@ -93,10 +99,7 @@ export default function LoadingPage() {
9399
console.log("Analysis result saved");
94100
console.log(processRes);
95101

96-
sessionStorage.setItem("biasScore", JSON.stringify(biasRes.data));
97-
98-
console.log("Bias score saved");
99-
console.log(biasRes);
102+
100103
// optional logging
101104
} catch (err) {
102105
console.error("Failed to process article:", err);

frontend/app/analyze/results/page.tsx

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ import { useState, useEffect, useRef } from "react";
55
import { useRouter } from "next/navigation";
66
import Link from "next/link";
77
import {
8-
ArrowLeft,
9-
Globe,
10-
MessageSquare,
118
Send,
12-
ThumbsDown,
13-
ThumbsUp,
14-
Menu,
159
Link as LinkIcon,
1610
} from "lucide-react";
1711
import { Button } from "@/components/ui/button";
@@ -23,12 +17,11 @@ import {
2317
CardHeader,
2418
CardTitle,
2519
} from "@/components/ui/card";
26-
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
2720
import { Input } from "@/components/ui/input";
2821
import { Badge } from "@/components/ui/badge";
29-
import { Separator } from "@/components/ui/separator";
3022
import BiasMeter from "@/components/bias-meter";
3123

24+
3225
/**
3326
* Renders the article analysis page with summary, perspectives, fact checks, bias meter, AI chat, and sources.
3427
*/
@@ -40,7 +33,6 @@ export default function AnalyzePage() {
4033
const [activeTab, setActiveTab] = useState("summary");
4134
const [message, setMessage] = useState("");
4235
const [isLoading, setIsLoading] = useState(true);
43-
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
4436
const [messages, setMessages] = useState<{ role: string; content: string }[]>(
4537
[
4638
{
@@ -52,54 +44,39 @@ export default function AnalyzePage() {
5244
);
5345

5446
useEffect(() => {
55-
const timer = setTimeout(() => setIsLoading(false), 1500);
56-
const storedData = sessionStorage.getItem("analysisResult");
5747
const storedBiasScore = sessionStorage.getItem("BiasScore");
48+
const storedData = sessionStorage.getItem("analysisResult");
49+
if (storedBiasScore && storedData){
50+
setIsLoading(false);
51+
}
52+
5853
if (storedBiasScore) setBiasScore(JSON.parse(storedBiasScore).bias_score);
5954
else console.warn("No bias score found.");
6055

6156
if (storedData) setAnalysisData(JSON.parse(storedData));
6257
else console.warn("No analysis result found");
63-
return () => clearTimeout(timer);
58+
6459
}, []);
6560

6661
useEffect(() => {
6762
if (isRedirecting.current) {
6863
return;
6964
}
7065

71-
const timer = setTimeout(() => setIsLoading(false), 1500);
66+
7267
const storedData = sessionStorage.getItem("analysisResult");
68+
const storedBiasScore = sessionStorage.getItem("BiasScore");
7369

74-
if (storedData) {
75-
const parsedData = JSON.parse(storedData);
76-
const requiredFields = [
77-
"cleaned_text",
78-
"facts",
79-
"sentiment",
80-
"perspective",
81-
"score",
82-
];
83-
const isDataValid = requiredFields.every(
84-
(field) => parsedData[field] !== undefined && parsedData[field] !== null
85-
);
86-
87-
if (isDataValid) {
88-
setAnalysisData(parsedData);
89-
} else {
90-
console.warn("Incomplete analysis data. Redirecting...");
91-
92-
isRedirecting.current = true;
93-
router.push("/analyze");
94-
}
95-
} else {
96-
console.warn("No analysis result found. Redirecting...");
97-
98-
isRedirecting.current = true;
99-
router.push("/analyze");
70+
if (storedBiasScore && storedData) {
71+
// inside here TS knows storedBiasScore and storedData are strings
72+
setBiasScore(JSON.parse(storedBiasScore).bias_score);
73+
setAnalysisData(JSON.parse(storedData));
74+
setIsLoading(false);
75+
} else {
76+
console.warn("No bias or data found. Redirecting...");
77+
router.push("/analyze");
10078
}
10179

102-
return () => clearTimeout(timer);
10380
}, [router]);
10481

10582
const handleSendMessage = (e: React.FormEvent) => {
@@ -120,7 +97,7 @@ export default function AnalyzePage() {
12097
}, 1000);
12198
};
12299

123-
if (isLoading || !analysisData) {
100+
if (isLoading || !analysisData || !biasScore) {
124101
return (
125102
<div className="flex items-center justify-center h-screen">
126103
<div className="text-muted-foreground">Analyzing content...</div>
@@ -156,7 +133,7 @@ export default function AnalyzePage() {
156133
</Badge>
157134
</div>
158135
<div className="bg-card rounded-lg border p-4 mb-8">
159-
<BiasMeter score={score} />
136+
<BiasMeter score={biasScore} />
160137
<p className="text-sm mt-2">Bias Score: {biasScore}</p>
161138
</div>
162139

0 commit comments

Comments
 (0)