File size: 3,612 Bytes
638afcf
 
 
 
 
 
 
 
c1b9b1f
638afcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import tempfile
from core import LightweightAnalyzer
from dataset_utils import contribute_to_dataset

# Get HF token from environment (set in HF Spaces secrets)
HF_TOKEN = os.getenv("HF_TOKEN")
HF_DATASET_REPO = "qurashiubaid/multimodal-materials-dataset"  # Change this!

analyzer = LightweightAnalyzer()

def process_files(xrd_file, vsm_file, uvvis_file, pl_file, sample_name, contribute):
    try:
        results = {"sample_name": sample_name}
        
        # Process each modality
        if xrd_file is not None:
            x, y = analyzer.load_csv(xrd_file.name)
            results['xrd'] = analyzer.analyze_xrd(x, y)
        
        if vsm_file is not None:
            x, y = analyzer.load_csv(vsm_file.name)
            results['vsm'] = analyzer.analyze_vsm(x, y)
        
        if uvvis_file is not None:
            x, y = analyzer.load_csv(uvvis_file.name)
            results['uvvis'] = analyzer.analyze_uvvis(x, y)
        
        if pl_file is not None:
            x, y = analyzer.load_csv(pl_file.name)
            results['pl'] = analyzer.analyze_pl(x, y)
        
        # Generate report
        report = analyzer.generate_report(results)
        
        # Contribute to dataset
        if contribute and HF_TOKEN:
            success, msg = contribute_to_dataset(
                results, sample_name, HF_DATASET_REPO, HF_TOKEN
            )
            if success:
                report += f"\n\n✅ {msg}"
            else:
                report += f"\n\n⚠️ {msg}"
        elif contribute:
            report += "\n\nℹ️ Dataset contribution requires HF token (not available in public demo)."
        
        # Generate plots
        with tempfile.TemporaryDirectory() as tmp_dir:
            plot_paths = analyzer.generate_plots(results, sample_name, tmp_dir)
            return report, plot_paths
            
    except Exception as e:
        return f"Error: {str(e)}", []

# Gradio interface
with gr.Blocks(title="Materials Analyzer") as demo:
    gr.Markdown("# 🔬 Multi-Modal Materials Analyzer")
    gr.Markdown("Lightweight analysis for XRD, VSM, UV-Vis, and PL data")
    
    with gr.Row():
        with gr.Column():
            sample_name = gr.Textbox(label="Sample Name", value="Sample1")
            
            xrd_file = gr.File(label="XRD CSV", file_types=[".csv"])
            vsm_file = gr.File(label="VSM CSV", file_types=[".csv"])
            uvvis_file = gr.File(label="UV-Vis CSV", file_types=[".csv"])
            pl_file = gr.File(label="PL CSV", file_types=[".csv"])
            
            contribute = gr.Checkbox(
                label="Contribute results to public dataset",
                value=False,
                interactive=bool(HF_TOKEN)
            )
            
            submit_btn = gr.Button("Analyze", variant="primary")
        
        with gr.Column():
            report = gr.Textbox(label="Analysis Report", lines=20)
            plots = gr.Gallery(label="Results", columns=2)
    
    submit_btn.click(
        process_files,
        [xrd_file, vsm_file, uvvis_file, pl_file, sample_name, contribute],
        [report, plots]
    )
    
    gr.Markdown("### ℹ️ Instructions")
    gr.Markdown("""
    **CSV Format:**
    - XRD: columns `2theta`, `intensity`
    - VSM: columns `H`, `M`
    - UV-Vis: columns `wavelength`, `absorption`
    - PL: columns `wavelength`, `intensity`
    
    **Note:** This is a lightweight demo. For full analysis with TEM and advanced features, 
    run locally with the complete pipeline.
    """)

if __name__ == "__main__":
    demo.launch()