Spaces:
Sleeping
Sleeping
Download modules/proposal_pdf.py from chemenovausa/orbitchem-api: direct link, hf CLI and curl.
- Browser
- Download file 16.4 kB
-
https://huggingface.co/spaces/chemenovausa/orbitchem-api/resolve/main/modules/proposal_pdf.py
- Command line
-
hf download hf://spaces/chemenovausa/orbitchem-api/modules/proposal_pdf.py
-
curl -L -o proposal_pdf.py https://huggingface.co/spaces/chemenovausa/orbitchem-api/resolve/main/modules/proposal_pdf.py
16.4 kB
| """ | |
| modules/proposal_pdf.py | |
| ChemeNova Γ OrbitChem β Branded Qualification Proposal PDF | |
| Generates a professional PDF report from Full Qualification Suite results. | |
| Uses reportlab (MIT) for PDF generation. | |
| Author: ChemeNova LLC β PROPRIETARY | |
| """ | |
| from __future__ import annotations | |
| import io | |
| from datetime import datetime | |
| from typing import Dict, Optional | |
| try: | |
| from reportlab.lib.pagesizes import A4 | |
| from reportlab.lib.units import mm | |
| from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle | |
| from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT | |
| from reportlab.lib import colors | |
| from reportlab.platypus import ( | |
| SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, | |
| HRFlowable, KeepTogether | |
| ) | |
| from reportlab.platypus.flowables import HRFlowable | |
| REPORTLAB_OK = True | |
| except ImportError: | |
| REPORTLAB_OK = False | |
| # ββ ChemeNova brand colours βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| CN_NAVY = colors.HexColor('#0A1628') | |
| CN_TEAL = colors.HexColor('#0D9488') | |
| CN_AMBER = colors.HexColor('#D97706') | |
| CN_WHITE = colors.white | |
| CN_LIGHT = colors.HexColor('#F0F2F5') | |
| CN_MUTED = colors.HexColor('#64748B') | |
| CN_GREEN = colors.HexColor('#1D9E75') | |
| CN_RED = colors.HexColor('#E24B4A') | |
| CN_WARN = colors.HexColor('#EF9F27') | |
| def _pass_color(passed: bool): | |
| return CN_GREEN if passed else CN_RED | |
| def _rating_color(rating: str): | |
| r = rating.lower() | |
| if r in ('excellent', 'good'): return CN_GREEN | |
| if r in ('marginal',): return CN_WARN | |
| return CN_RED | |
| def generate_qualification_pdf( | |
| blend: Dict[str, float], | |
| orbit: str, | |
| mission_years: float, | |
| substrate: str, | |
| outgassing, | |
| radiation, | |
| thermal, | |
| org_name: str = "ChemeNova LLC", | |
| ) -> bytes: | |
| """ | |
| Generate a branded ChemeNova qualification proposal PDF. | |
| Returns bytes suitable for st.download_button. | |
| """ | |
| if not REPORTLAB_OK: | |
| raise ImportError("reportlab not installed. Run: pip install reportlab") | |
| buf = io.BytesIO() | |
| doc = SimpleDocTemplate( | |
| buf, pagesize=A4, | |
| leftMargin=18*mm, rightMargin=18*mm, | |
| topMargin=14*mm, bottomMargin=18*mm, | |
| ) | |
| styles = getSampleStyleSheet() | |
| W = A4[0] - 36*mm # usable width | |
| # ββ Custom styles βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _sty(name, parent='Normal', **kw): | |
| s = ParagraphStyle(name, parent=styles[parent], **kw) | |
| return s | |
| H1 = _sty('H1', fontSize=22, textColor=CN_WHITE, fontName='Helvetica-Bold', | |
| spaceAfter=4, leading=26) | |
| H2 = _sty('H2', fontSize=13, textColor=CN_TEAL, fontName='Helvetica-Bold', | |
| spaceBefore=10, spaceAfter=4) | |
| H3 = _sty('H3', fontSize=10, textColor=CN_AMBER, fontName='Helvetica-Bold', | |
| spaceBefore=6, spaceAfter=2) | |
| BODY= _sty('BODY', fontSize=9, textColor=colors.HexColor('#1e293b'), | |
| leading=13, spaceAfter=3) | |
| SMALL=_sty('SMALL',fontSize=7.5,textColor=CN_MUTED, leading=10) | |
| CAP = _sty('CAP', fontSize=8, textColor=CN_MUTED, fontName='Helvetica-Oblique') | |
| RHD = _sty('RHD', fontSize=8, textColor=CN_WHITE, fontName='Helvetica-Bold', | |
| alignment=TA_CENTER) | |
| RVAL= _sty('RVAL', fontSize=8.5,textColor=colors.HexColor('#0f172a'), | |
| alignment=TA_CENTER) | |
| ts_hdr = TableStyle([ | |
| ('BACKGROUND', (0,0), (-1,0), CN_NAVY), | |
| ('TEXTCOLOR', (0,0), (-1,0), CN_WHITE), | |
| ('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'), | |
| ('FONTSIZE', (0,0), (-1,0), 8), | |
| ('ROWBACKGROUNDS',(0,1),(-1,-1),[CN_LIGHT, CN_WHITE]), | |
| ('FONTSIZE', (0,1), (-1,-1), 8), | |
| ('GRID', (0,0), (-1,-1), 0.3, colors.HexColor('#CBD5E1')), | |
| ('ALIGN', (1,0), (-1,-1), 'CENTER'), | |
| ('TOPPADDING', (0,0), (-1,-1), 4), | |
| ('BOTTOMPADDING',(0,0),(-1,-1), 4), | |
| ('LEFTPADDING', (0,0), (-1,-1), 6), | |
| ('RIGHTPADDING',(0,0),(-1,-1), 6), | |
| ]) | |
| story = [] | |
| # ββ Header banner βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| banner_data = [[ | |
| Paragraph('<font color="white"><b>OrbitChemβ’</b></font>', H1), | |
| Paragraph( | |
| f'<font color="#D97706"><b>SPACECRAFT MATERIAL<br/>QUALIFICATION REPORT</b></font>', | |
| _sty('BT', fontSize=11, textColor=CN_AMBER, fontName='Helvetica-Bold', | |
| alignment=TA_RIGHT, leading=15)), | |
| ]] | |
| banner = Table(banner_data, colWidths=[W*0.6, W*0.4]) | |
| banner.setStyle(TableStyle([ | |
| ('BACKGROUND', (0,0),(-1,-1), CN_NAVY), | |
| ('VALIGN', (0,0),(-1,-1), 'MIDDLE'), | |
| ('TOPPADDING', (0,0),(-1,-1), 10), | |
| ('BOTTOMPADDING', (0,0),(-1,-1), 10), | |
| ('LEFTPADDING', (0,0),(-1,-1), 12), | |
| ('RIGHTPADDING', (0,0),(-1,-1), 12), | |
| ('ROUNDEDCORNERS',(0,0),(-1,-1), [6,6,6,6]), | |
| ])) | |
| story.append(banner) | |
| story.append(Spacer(1, 4*mm)) | |
| # Sub-header | |
| meta = Table([[ | |
| Paragraph(f'<b>Prepared for:</b> {org_name}', BODY), | |
| Paragraph(f'<b>Date:</b> {datetime.now().strftime("%d %B %Y")}', BODY), | |
| Paragraph(f'<b>Orbit:</b> {orbit} Β· <b>Mission:</b> {mission_years:.1f} yr Β· <b>Substrate:</b> {substrate.replace("_"," ").title()}', BODY), | |
| ]], colWidths=[W*0.35, W*0.25, W*0.40]) | |
| meta.setStyle(TableStyle([ | |
| ('BACKGROUND', (0,0),(-1,-1), colors.HexColor('#F8FAFC')), | |
| ('GRID', (0,0),(-1,-1), 0.3, colors.HexColor('#E2E8F0')), | |
| ('TOPPADDING', (0,0),(-1,-1), 5), | |
| ('BOTTOMPADDING',(0,0),(-1,-1), 5), | |
| ('LEFTPADDING', (0,0),(-1,-1), 8), | |
| ])) | |
| story.append(meta) | |
| story.append(Spacer(1, 5*mm)) | |
| # ββ Qualification Summary ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| story.append(Paragraph('1. Qualification Summary', H2)) | |
| story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) | |
| story.append(Spacer(1, 2*mm)) | |
| def verdict(passed, label): | |
| col = CN_GREEN if passed else CN_RED | |
| sym = 'β PASS' if passed else 'β FAIL' | |
| return Paragraph(f'<font color="{"#1D9E75" if passed else "#E24B4A"}"><b>{sym}</b></font>', RVAL), label | |
| summary_rows = [ | |
| [Paragraph('Standard', RHD), Paragraph('Result', RHD), | |
| Paragraph('Key Metric', RHD), Paragraph('Value', RHD)], | |
| ['NASA MSFC-SPEC-1238 (Outgassing)', | |
| Paragraph(f'<font color="{"#1D9E75" if outgassing.nasa_pass else "#E24B4A"}"><b>{"β PASS" if outgassing.nasa_pass else "β FAIL"}</b></font>', RVAL), | |
| 'TML / CVCM', f'{outgassing.blend_tml_pct:.4f}% / {outgassing.blend_cvcm_pct:.5f}%'], | |
| ['ESA ECSS-Q-ST-70-02 (Outgassing)', | |
| Paragraph(f'<font color="{"#1D9E75" if outgassing.esa_pass else "#E24B4A"}"><b>{"β PASS" if outgassing.esa_pass else "β FAIL"}</b></font>', RVAL), | |
| 'VCM', f'{outgassing.blend_vcm_pct:.5f}%'], | |
| ['Radiation Stability (G-value model)', | |
| Paragraph(f'<font color="{"#1D9E75" if radiation.overall_rating in ("excellent","good") else "#EF9F27" if radiation.overall_rating=="marginal" else "#E24B4A"}"><b>{radiation.overall_rating.upper()}</b></font>', RVAL), | |
| 'Tensile retention', f'{radiation.tensile_retention_pct:.1f}% @ {radiation.total_tid_krad:.0f} kRad'], | |
| ['Thermal Cycling (Coffin-Manson)', | |
| Paragraph(f'<font color="{"#1D9E75" if thermal.blend_passes else "#E24B4A"}"><b>{"β PASS" if thermal.blend_passes else "β FAIL"}</b></font>', RVAL), | |
| 'Safety factor (min Nf / cycles)', f'{thermal.safety_factor:.2f}Γ'], | |
| ] | |
| sum_t = Table(summary_rows, colWidths=[W*0.40, W*0.15, W*0.28, W*0.17]) | |
| sum_t.setStyle(ts_hdr) | |
| story.append(sum_t) | |
| story.append(Spacer(1, 5*mm)) | |
| # ββ Blend Composition βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| story.append(Paragraph('2. Blend Composition', H2)) | |
| story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) | |
| story.append(Spacer(1, 2*mm)) | |
| blend_rows = [[Paragraph('Material', RHD), Paragraph('wt %', RHD), | |
| Paragraph('VP (Pa@25Β°C)', RHD), Paragraph('Outgas Risk', RHD)]] | |
| for rec in outgassing.records: | |
| risk_col = {'low':'#1D9E75','medium':'#EF9F27','high':'#E24B4A','critical':'#7c0000'}.get(rec.risk_level,'#64748B') | |
| blend_rows.append([ | |
| rec.ingredient, | |
| f'{rec.pct:.1f}%', | |
| f'{rec.vapour_pressure_Pa:.2e}', | |
| Paragraph(f'<font color="{risk_col}"><b>{rec.risk_level.upper()}</b></font>', RVAL), | |
| ]) | |
| blend_t = Table(blend_rows, colWidths=[W*0.50, W*0.10, W*0.20, W*0.20]) | |
| blend_t.setStyle(ts_hdr) | |
| story.append(blend_t) | |
| story.append(Spacer(1, 5*mm)) | |
| # ββ Outgassing Detail βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| story.append(Paragraph('3. Outgassing Screen β ASTM E595 Conditions (125Β°C / 24h / 10β»β΄ Pa)', H2)) | |
| story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) | |
| story.append(Spacer(1, 2*mm)) | |
| story.append(Paragraph( | |
| f'Blend TML = <b>{outgassing.blend_tml_pct:.4f}%</b> (limit β€ 1.0%) Β· ' | |
| f'Blend CVCM = <b>{outgassing.blend_cvcm_pct:.5f}%</b> (limit β€ 0.1%) Β· ' | |
| f'Blend VCM = <b>{outgassing.blend_vcm_pct:.5f}%</b> (ESA limit β€ 0.1%)', BODY)) | |
| story.append(Spacer(1, 2*mm)) | |
| og_rows = [[Paragraph(h, RHD) for h in ['Material','wt%','TML contrib %','CVCM contrib %','MW g/mol','Risk']]] | |
| for r in outgassing.records: | |
| og_rows.append([r.ingredient, f'{r.pct:.1f}%', f'{r.tml_contribution_pct:.5f}', | |
| f'{r.cvcm_contribution_pct:.6f}', f'{r.mw:.0f}', r.risk_level.upper()]) | |
| og_t = Table(og_rows, colWidths=[W*0.38,W*0.07,W*0.15,W*0.16,W*0.10,W*0.14]) | |
| og_t.setStyle(ts_hdr) | |
| story.append(og_t) | |
| for w in outgassing.notes: | |
| story.append(Paragraph(f'β’ {w}', SMALL)) | |
| story.append(Spacer(1, 5*mm)) | |
| # ββ Radiation βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| story.append(Paragraph(f'4. Radiation Stability β {radiation.environment} ({radiation.mission_years:.0f} yr, {radiation.total_tid_krad:.0f} kRad)', H2)) | |
| story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) | |
| story.append(Spacer(1, 2*mm)) | |
| story.append(Paragraph( | |
| f'Blend tensile retention = <b>{radiation.tensile_retention_pct:.1f}%</b> Β· ' | |
| f'Yellowing index = <b>{radiation.yellowing_index:.2f}</b> Β· ' | |
| f'Overall rating = <b>{radiation.overall_rating.upper()}</b>', BODY)) | |
| story.append(Spacer(1, 2*mm)) | |
| rad_rows = [[Paragraph(h, RHD) for h in ['Material','wt%','Polymer class','G(S)','G(X)','Net G','Tensile ret.','Rating']]] | |
| for r in radiation.records: | |
| rad_rows.append([ | |
| r.ingredient, f'{r.pct:.1f}%', r.polymer_class, | |
| f'{r.G_scission:.1f}', f'{r.G_crosslink:.1f}', f'{r.net_G:.2f}', | |
| f'{r.dose_response*100:.1f}%', r.rad_hardness.upper() | |
| ]) | |
| rad_t = Table(rad_rows, colWidths=[W*0.28,W*0.06,W*0.13,W*0.07,W*0.07,W*0.07,W*0.13,W*0.19]) | |
| rad_t.setStyle(ts_hdr) | |
| story.append(rad_t) | |
| for rec in radiation.recommendations: | |
| story.append(Paragraph(f'β’ {rec}', SMALL)) | |
| story.append(Spacer(1, 5*mm)) | |
| # ββ Thermal Cycling βββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| story.append(Paragraph(f'5. Thermal Cycling β {thermal.orbit} (ΞT={thermal.delta_T:.0f}Β°C, {thermal.total_cycles:,} cycles)', H2)) | |
| story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) | |
| story.append(Spacer(1, 2*mm)) | |
| story.append(Paragraph( | |
| f'Blend CTE = <b>{thermal.blend_cte_ppm_K:.1f} ppm/K</b> Β· ' | |
| f'Min Nf = <b>{int(min(thermal.blend_min_Nf,1e9)):,} cycles</b> Β· ' | |
| f'Safety factor = <b>{thermal.safety_factor:.2f}Γ</b> Β· ' | |
| f'Result = <b>{"PASS" if thermal.blend_passes else "FAIL"}</b>', BODY)) | |
| story.append(Spacer(1, 2*mm)) | |
| tc_rows = [[Paragraph(h, RHD) for h in ['Material','wt%','CTE ppm/K','E (GPa)','Nf cycles','Delam. G (J/mΒ²)','Risk']]] | |
| for r in thermal.records: | |
| tc_rows.append([ | |
| r.ingredient, f'{r.pct:.1f}%', f'{r.cte_ppm_K:.1f}', | |
| f'{r.modulus_GPa:.2f}', f'{int(min(r.cycles_to_fail,1e9)):,}', | |
| f'{r.delamination_GR:.1f}', r.risk_level.upper() | |
| ]) | |
| tc_t = Table(tc_rows, colWidths=[W*0.32,W*0.07,W*0.11,W*0.09,W*0.14,W*0.14,W*0.13]) | |
| tc_t.setStyle(ts_hdr) | |
| story.append(tc_t) | |
| for rec in thermal.recommendations: | |
| story.append(Paragraph(f'β’ {rec}', SMALL)) | |
| story.append(Spacer(1, 5*mm)) | |
| # ββ Recommendations βββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| all_recs = list(outgassing.warnings) + list(radiation.recommendations) + list(thermal.recommendations) | |
| if all_recs: | |
| story.append(Paragraph('6. Recommendations & Next Steps', H2)) | |
| story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) | |
| story.append(Spacer(1, 2*mm)) | |
| for i, rec in enumerate(all_recs[:8], 1): | |
| story.append(Paragraph(f'<b>{i}.</b> {rec}', BODY)) | |
| story.append(Spacer(1, 3*mm)) | |
| story.append(Paragraph('7. Qualification Pathway', H2)) | |
| story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) | |
| story.append(Spacer(1, 2*mm)) | |
| story.append(Paragraph( | |
| 'This report constitutes a <b>screening-level qualification assessment</b> using physics-based ' | |
| 'surrogate models (Clausius-Clapeyron outgassing, G-value radiation, Coffin-Manson thermal fatigue). ' | |
| 'Formal flight qualification requires:', BODY)) | |
| steps = [ | |
| ('Outgassing', 'ASTM E595-22 coupon test at 125Β°C / 24h / 7Γ10β»β΅ Torr β TML β€ 1.0%, CVCM β€ 0.1%'), | |
| ('Radiation', 'Co-60 gamma or proton beam TID test to mission dose + 25% margin'), | |
| ('Thermal', 'Thermal vacuum cycling per NASA-STD-7001 / ECSS-E-ST-32 β 100+ cycles minimum'), | |
| ('Mechanical', 'Random vibration + shock per MIL-STD-810 / ECSS-E-ST-32'), | |
| ] | |
| step_rows = [[Paragraph('<b>Test</b>', RHD), Paragraph('<b>Standard / Requirement</b>', RHD)]] | |
| for s, d in steps: | |
| step_rows.append([s, d]) | |
| step_t = Table(step_rows, colWidths=[W*0.20, W*0.80]) | |
| step_t.setStyle(ts_hdr) | |
| story.append(step_t) | |
| story.append(Spacer(1, 5*mm)) | |
| # ββ Footer ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| story.append(HRFlowable(width=W, thickness=1.0, color=CN_TEAL)) | |
| story.append(Spacer(1, 2*mm)) | |
| footer_data = [[ | |
| Paragraph('<b>OrbitChemβ’</b> Β· ChemeNova LLC Β· chemenova.com/orbitchem', SMALL), | |
| Paragraph(f'Generated {datetime.now().strftime("%Y-%m-%d %H:%M UTC")}', _sty('FR', fontSize=7.5, textColor=CN_MUTED, alignment=TA_RIGHT)), | |
| ]] | |
| footer_t = Table(footer_data, colWidths=[W*0.65, W*0.35]) | |
| footer_t.setStyle(TableStyle([('TOPPADDING',(0,0),(-1,-1),2)])) | |
| story.append(footer_t) | |
| story.append(Paragraph( | |
| 'β This report is generated by OrbitChemβ’ AI screening models and does not constitute formal ' | |
| 'flight qualification. All results must be verified by laboratory testing before use in space hardware.', | |
| _sty('DISC', fontSize=6.5, textColor=CN_MUTED, fontName='Helvetica-Oblique'))) | |
| doc.build(story) | |
| buf.seek(0) | |
| return buf.read() | |