Spaces:
Paused
Paused
| import pandas as pd | |
| import gradio as gr | |
| from transformers import pipeline | |
| MODEL_ID = "mcsqstudio/africa-sector-classifier" | |
| SECTOR_NAMES = { | |
| "ATX": "Agritech", | |
| "ETX": "Edtech", | |
| "HTX": "Healthtech", | |
| "FTX": "Fintech", | |
| "REC": "Retail & E-commerce", | |
| "ERG": "Energy", | |
| "MFG": "Manufacturing", | |
| "XSC": "Cross-sector", | |
| } | |
| _pipe = None | |
| _model_error = None | |
| def get_pipe(): | |
| global _pipe, _model_error | |
| if _pipe is None: | |
| try: | |
| _pipe = pipeline("text-classification", model=MODEL_ID, truncation=True) | |
| _model_error = None | |
| except Exception as exc: | |
| _model_error = str(exc) | |
| return _pipe | |
| def classify(text): | |
| if not text or not text.strip(): | |
| return pd.DataFrame(), "Please enter a company description." | |
| pipe = get_pipe() | |
| if pipe is None: | |
| return pd.DataFrame(), ( | |
| "Model not ready yet. Run **Sita_Sector_Model_v2_transformers.ipynb** " | |
| "in Colab to fine-tune and push `mcsqstudio/africa-sector-classifier` " | |
| "to Hugging Face, then click Classify again.\n\n" | |
| f"Detail: {_model_error}" | |
| ) | |
| results = pipe(text.strip()[:2000]) | |
| rows = [ | |
| { | |
| "Sector": r["label"], | |
| "Sector name": SECTOR_NAMES.get(r["label"], ""), | |
| "Confidence": round(r["score"], 4), | |
| } | |
| for r in results | |
| ] | |
| df = pd.DataFrame(rows) | |
| top = rows[0] | |
| headline = ( | |
| f"**{top['Sector']} - {SECTOR_NAMES.get(top['Sector'], '')}** " | |
| f"({top['Confidence']:.1%} confidence)" | |
| ) | |
| return df, headline | |
| EXAMPLES = [ | |
| "Mobile payment platform enabling small businesses to accept card payments in Nairobi", | |
| "Solar-powered microgrids bringing affordable electricity to rural communities in Uganda", | |
| "Online marketplace connecting farmers directly to buyers and aggregating harvest data", | |
| "AI tutoring app that personalizes math lessons for secondary school students in Nigeria", | |
| "Telemedicine service offering remote consultations with licensed doctors in Kenya", | |
| ] | |
| with gr.Blocks(title="Sita Sector Classifier") as demo: | |
| gr.Markdown( | |
| "# Sita Sector Classifier\n" | |
| "Classifies an African startup description into one of seven Sita Sector industries. " | |
| "Fine-tuned DistilBERT on the " | |
| "[Africa Startup Directory](https://huggingface.co/datasets/mcsqstudio/africa-startup-directory).\n\n" | |
| "**Sectors:** ATX Agritech - ETX Edtech - HTX Healthtech - FTX Fintech - " | |
| "REC Retail & E-commerce - ERG Energy - MFG Manufacturing" | |
| ) | |
| txt = gr.Textbox(label="Company description", lines=4) | |
| btn = gr.Button("Classify", variant="primary") | |
| headline = gr.Markdown() | |
| out = gr.Dataframe( | |
| headers=["Sector", "Sector name", "Confidence"], | |
| datatype=["str", "str", "number"], | |
| interactive=False, | |
| ) | |
| btn.click(classify, inputs=txt, outputs=[out, headline]) | |
| gr.Examples(examples=EXAMPLES, inputs=txt) | |
| demo.launch() | |