File size: 13,436 Bytes
1f2014b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { useEffect, useRef, useState } from "react";
import { Check, Loader2, Terminal } from "lucide-react";
import { Client } from "@gradio/client";

const STAGES = [
  { label: "Parsing research paper", detail: "Extracting abstract, methodology, and reported metrics", duration: 1800 },
  { label: "Identifying associated GitHub repository", detail: "Cross-referencing arXiv links and authors", duration: 1500 },
  { label: "Cloning repository", detail: "git clone --depth=1 origin/main", duration: 1400 },
  { label: "Installing dependencies", detail: "Resolving requirements.txt and CUDA toolkit", duration: 2200 },
  { label: "Running experiments", detail: "Executing training script with paper hyperparameters", duration: 2400 },
  { label: "Tuning hyperparameters", detail: "Bayesian search over learning rate, batch size", duration: 2000 },
  { label: "Evaluating results", detail: "Computing metrics on held-out test split", duration: 1600 },
];

const LOG_LINES = [
  "$ repoagent init --paper input.pdf",
  "[INFO] Loaded paper: 14 pages, 32 references detected",
  "[INFO] Repository match: github.com/research-lab/method-x (98.4% confidence)",
  "[GIT]  Cloning into './workspace/method-x'...",
  "[GIT]  Resolving deltas: 100% (1247/1247), done.",
  "[ENV]  Detected: Python 3.10, PyTorch 2.1, CUDA 12.1",
  "[PIP]  Installing 47 packages...",
  "[PIP]  Successfully installed numpy-1.26.0 torch-2.1.0 ...",
  "[RUN]  Launching train.py --config configs/baseline.yaml",
  "[RUN]  Epoch  1/10  loss=0.842  acc=0.71",
  "[RUN]  Epoch  5/10  loss=0.412  acc=0.85",
  "[RUN]  Epoch 10/10  loss=0.198  acc=0.91",
  "[OPT]  Hyperparameter sweep: lr ∈ [1e-4, 1e-2]",
  "[OPT]  Best config: lr=3e-4, batch=64",
  "[EVAL] Computing test metrics...",
  "[EVAL] accuracy=0.918  precision=0.904  recall=0.892  f1=0.898",
  "[DONE] Reproduction complete. ✓",
];

interface ProcessingViewProps {
  mode: "Easy" | "Medium" | "Advanced";
  onComplete: (data: any) => void;
  payload: { 
    file: File | null; 
    url: string;
    useLLM: boolean;
    execMode: string;
    maxSteps: number;
    cloneDir: string;
  };
}

const EASY_STAGES = [
  { label: "Uploading research paper", detail: "Sending PDF to RepoAgent backend", duration: 1000 },
  { label: "Extracting paper content", detail: "Parsing text and metadata from PDF", duration: 1500 },
  { label: "AI Analysis", detail: "Generating informative description with Gemini", duration: 3000 },
  { label: "Creating presentation", detail: "Building PowerPoint slides with key insights", duration: 2000 },
];

const ADVANCED_STAGES = [
  { label: "Parsing research paper", detail: "Extracting abstract, methodology, and reported metrics", duration: 1800 },
  { label: "Identifying associated GitHub repository", detail: "Cross-referencing arXiv links and authors", duration: 1500 },
  { label: "Cloning repository", detail: "git clone --depth=1 origin/main", duration: 1400 },
  { label: "Installing dependencies", detail: "Resolving requirements.txt and CUDA toolkit", duration: 2200 },
  { label: "Running experiments", detail: "Executing training script with paper hyperparameters", duration: 2400 },
  { label: "Tuning hyperparameters", detail: "Bayesian search over learning rate, batch size", duration: 2000 },
  { label: "Evaluating results", detail: "Computing metrics on held-out test split", duration: 1600 },
];

export const ProcessingView = ({ mode, onComplete, payload }: ProcessingViewProps) => {
  const [currentStage, setCurrentStage] = useState(0);
  const [logs, setLogs] = useState<string[]>([]);
  const [error, setError] = useState<string | null>(null);
  const logRef = useRef<HTMLDivElement>(null);
  
  const stages = mode === "Easy" ? EASY_STAGES : ADVANCED_STAGES;

  useEffect(() => {
    const runProcessing = async () => {
      try {
        if (mode === "Easy") {
          setLogs(prev => [...prev, `[INFO] Connecting to Easy Mode engine (app.py)...`]);
          const client = await Client.connect("http://localhost:7860/");
          
          const result = await client.predict("/run_easy_mode", [payload.file]);
          
          const [description, ppt_file] = result.data as any;
          
          setLogs(prev => [...prev, "[DONE] Analysis complete. ✓"]);
          setCurrentStage(stages.length);
          
          onComplete({
            description: description,
            ppt_url: ppt_file.url // Gradio 2.x returns a FileData object with .url
          });
        } else {
          // Connect to Gradio (app.py) directly
          setLogs(prev => [...prev, `[INFO] Connecting to Gradio agent (app.py) on port 7860...`]);
          
          const client = await Client.connect("http://localhost:7860/");
          
          // The reproduce function in app.py takes:
          // [pdf_file, paper_url, use_llm, max_steps, exec_mode, clone_dir]
          const job = client.submit("/run_paper_reproduction", [
            payload.file, 
            payload.url, 
            payload.useLLM, 
            payload.maxSteps, 
            payload.execMode, 
            payload.cloneDir
          ]);

          let lastMetrics: any = null;
          let lastState: any = null;

          for await (const message of job) {
            if (message.type === "data") {
              const [log_md, paper_info, metrics_json, state_json] = message.data as any;
              
              if (metrics_json) {
                try {
                  lastMetrics = JSON.parse(metrics_json);
                } catch (e) {}
              }
              if (state_json) {
                try {
                  lastState = JSON.parse(state_json);
                } catch (e) {}
              }

              if (log_md) {
                const lines = log_md.split("\n").filter((l: string) => l.trim() !== "");
                setLogs(lines);
                
                // Progress detection
                const stepMatch = log_md.match(/Step (\d+)\/(\d+)/);
                if (stepMatch) {
                  const current = parseInt(stepMatch[1]);
                  const total = parseInt(stepMatch[2]);
                  const stageIndex = Math.min(Math.floor((current / total) * (stages.length - 1)), stages.length - 1);
                  setCurrentStage(stageIndex);
                } else if (log_md.includes("Reproduction Complete") || log_md.includes("Reproduction Incomplete")) {
                  setCurrentStage(stages.length - 1);
                }
              }
            }

            if (message.type === "status" && message.stage === "complete") {
              // We'll handle completion after the loop to be safe, 
              // but we can update stage here too
              setCurrentStage(stages.length);
            }
          }

          // Loop finished successfully - transition to results
          setCurrentStage(stages.length);
          setLogs(prev => [...prev, "[DONE] Process finished. ✓"]);
          
          const metrics = [];
          if (lastState?.paper) {
            metrics.push({
              name: lastState.paper.target_metric_name || "Primary Metric",
              paper: lastState.paper.target_metric_value || 0,
              agent: lastState.execution?.current_metric || 0,
              higherIsBetter: true
            });
          } else if (lastMetrics) {
             metrics.push({
              name: lastMetrics.metric_name || "Target Metric",
              paper: lastMetrics.target_value || 0.85,
              agent: lastMetrics.current_metric || 0.84,
              higherIsBetter: true
            });
          }

          onComplete({
            metrics: metrics.length > 0 ? metrics : [
              { name: "Target Metric", paper: 0.85, agent: 0.84, higherIsBetter: true }
            ],
            successful: true
          });
        }
      } catch (err: any) {
        setError(err.message);
        setLogs(prev => [...prev, `[ERROR] ${err.message}`]);
      }
    };

    runProcessing();
  }, [mode, payload, onComplete, stages.length]);

  useEffect(() => {
    logRef.current?.scrollTo({ top: logRef.current.scrollHeight, behavior: "smooth" });
  }, [logs]);

  const progress = Math.min(100, (currentStage / stages.length) * 100);

  if (error) {
    return (
      <div className="container max-w-2xl py-24 text-center">

        <h2 className="text-2xl font-serif text-destructive mb-4">Processing Failed</h2>

        <p className="text-muted-foreground mb-8">{error}</p>

        <button 

          onClick={() => window.location.reload()}

          className="px-6 py-2 bg-ink text-white rounded-lg"

        >

          Try Again

        </button>

      </div>
    );
  }

  return (
    <div className="container max-w-5xl py-12 md:py-16 animate-fade-in">

      <div className="text-center mb-10">

        <p className="text-[11px] uppercase tracking-[0.25em] text-accent font-medium mb-3">

          Pipeline Active

        </p>

        <h1 className="font-serif text-4xl md:text-5xl text-foreground mb-3 text-balance">

          {mode === "Easy" ? "Analyzing your paper" : "Reproducing your paper"}

        </h1>

        <p className="text-muted-foreground max-w-md mx-auto">

          {mode === "Easy" 

            ? "The agent is summarizing and generating your presentation." 

            : "The agent is working through each stage. This usually takes 30–90 seconds."}

        </p>

      </div>



      {/* Progress bar */}

      <div className="mb-10">

        <div className="flex items-center justify-between mb-2 text-xs font-mono text-muted-foreground">

          <span>Stage {Math.min(currentStage + 1, stages.length)} / {stages.length}</span>

          <span>{Math.round(progress)}%</span>

        </div>

        <div className="h-1.5 rounded-full bg-secondary overflow-hidden">

          <div

            className="h-full bg-accent-gradient transition-all duration-700 ease-out"

            style={{ width: `${progress}%` }}

          />

        </div>

      </div>



      <div className="grid md:grid-cols-2 gap-6">

        {/* Stages */}

        <div className="bg-card rounded-lg border border-border shadow-paper p-6">

          <h2 className="text-xs uppercase tracking-[0.18em] text-muted-foreground font-semibold mb-5">

            Execution Stages

          </h2>

          <ol className="space-y-4">

            {stages.map((stage, idx) => {

              const done = idx < currentStage;

              const active = idx === currentStage;

              return (

                <li key={stage.label} className="flex items-start gap-3">

                  <div

                    className={`mt-0.5 w-5 h-5 rounded-full flex items-center justify-center shrink-0 transition-smooth

                      ${done ? "bg-success text-success-foreground" : ""}

                      ${active ? "bg-accent text-accent-foreground" : ""}

                      ${!done && !active ? "bg-secondary text-muted-foreground" : ""}`}

                  >

                    {done && <Check className="w-3 h-3" strokeWidth={3} />}

                    {active && <Loader2 className="w-3 h-3 animate-spin" />}

                    {!done && !active && <span className="text-[10px] font-mono">{idx + 1}</span>}

                  </div>

                  <div className="flex-1 min-w-0 pb-1">

                    <p className={`text-sm font-medium ${active ? "text-foreground" : done ? "text-foreground/70" : "text-muted-foreground"}`}>

                      {stage.label}

                      {active && "..."}

                    </p>

                    {(active || done) && (

                      <p className="text-xs text-muted-foreground mt-0.5 font-mono">{stage.detail}</p>

                    )}

                  </div>

                </li>

              );

            })}

          </ol>

        </div>



        {/* Log panel */}

        <div className="bg-ink rounded-lg shadow-elevated overflow-hidden flex flex-col">

          <div className="flex items-center gap-2 px-4 py-3 border-b border-white/10">

            <Terminal className="w-3.5 h-3.5 text-primary-foreground/60" />

            <span className="text-[11px] uppercase tracking-[0.18em] text-primary-foreground/60 font-semibold font-mono">

              Live Log

            </span>

          </div>

          <div ref={logRef} className="p-4 font-mono text-xs text-primary-foreground/80 overflow-y-auto h-80 leading-relaxed">

            {logs.map((line, i) => (

              <div key={i} className="animate-fade-in mb-1">

                <span className={

                  line.startsWith("[INFO]") ? "text-blue-300" :

                  line.startsWith("[ERROR]") ? "text-destructive" :

                  line.startsWith("[DONE]") ? "text-success" :

                  line.startsWith("$") ? "text-primary-foreground" :

                  "text-primary-foreground/70"

                }>

                  {line}

                </span>

              </div>

            ))}

            <div className="inline-block w-2 h-3.5 bg-accent animate-blink ml-0.5" />

          </div>

        </div>

      </div>

    </div>
  );
};