Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from transformers import BertTokenizer, BertForSequenceClassification, pipeline
|
| 5 |
+
|
| 6 |
+
# =========================
|
| 7 |
+
# Load BERT Model
|
| 8 |
+
# =========================
|
| 9 |
+
model_name = "Omnia-cy/bert_model_1"
|
| 10 |
+
|
| 11 |
+
tokenizer = BertTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = BertForSequenceClassification.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
model.eval()
|
| 15 |
+
|
| 16 |
+
# =========================
|
| 17 |
+
# Load Zero-Shot Model
|
| 18 |
+
# =========================
|
| 19 |
+
zero_shot = pipeline(
|
| 20 |
+
"zero-shot-classification",
|
| 21 |
+
model="facebook/bart-large-mnli"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
labels = [
|
| 25 |
+
"This problem can be solved using software or AI",
|
| 26 |
+
"This problem cannot be solved using software or AI"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
# =========================
|
| 30 |
+
# Prediction Function
|
| 31 |
+
# =========================
|
| 32 |
+
def analyze_problem(sector, subsector, ptype, target, description):
|
| 33 |
+
|
| 34 |
+
# نفس الفورمات بتاع النوتبوك
|
| 35 |
+
text = (
|
| 36 |
+
f"Sector: {sector}. "
|
| 37 |
+
f"Subsector: {subsector}. "
|
| 38 |
+
f"Type: {ptype}. "
|
| 39 |
+
f"Target Group: {target}. "
|
| 40 |
+
f"Description: {description}."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# -------- BERT --------
|
| 44 |
+
inputs = tokenizer(
|
| 45 |
+
text,
|
| 46 |
+
return_tensors="pt",
|
| 47 |
+
truncation=True,
|
| 48 |
+
padding=True,
|
| 49 |
+
max_length=256
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
outputs = model(**inputs)
|
| 54 |
+
|
| 55 |
+
probs = F.softmax(outputs.logits, dim=1)
|
| 56 |
+
ai_prob = probs[0][1].item()
|
| 57 |
+
|
| 58 |
+
# -------- Decision --------
|
| 59 |
+
threshold = 0.8
|
| 60 |
+
|
| 61 |
+
if ai_prob >= threshold:
|
| 62 |
+
return f"✅ AI Probability: {ai_prob:.2f}\nResult: Solvable using AI"
|
| 63 |
+
|
| 64 |
+
else:
|
| 65 |
+
# -------- Zero-Shot --------
|
| 66 |
+
result = zero_shot(text, candidate_labels=labels)
|
| 67 |
+
|
| 68 |
+
final_label = result["labels"][0]
|
| 69 |
+
|
| 70 |
+
return f"""
|
| 71 |
+
AI Probability: {ai_prob:.2f}
|
| 72 |
+
|
| 73 |
+
Zero-Shot Result:
|
| 74 |
+
{final_label}
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
# =========================
|
| 78 |
+
# UI
|
| 79 |
+
# =========================
|
| 80 |
+
iface = gr.Interface(
|
| 81 |
+
fn=analyze_problem,
|
| 82 |
+
inputs=[
|
| 83 |
+
gr.Textbox(label="Sector"),
|
| 84 |
+
gr.Textbox(label="Subsector"),
|
| 85 |
+
gr.Textbox(label="Type"),
|
| 86 |
+
gr.Textbox(label="Target Group"),
|
| 87 |
+
gr.Textbox(label="Problem Description")
|
| 88 |
+
],
|
| 89 |
+
outputs=gr.Textbox(label="Result"),
|
| 90 |
+
title="AI Problem Analyzer",
|
| 91 |
+
description="Check if a problem can be solved using AI or software"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
iface.launch()
|