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()