Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# বাংলা মন্তব্য সহ Hugging Face Space deploy-ready কোড
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# Hybrid Model Design
|
| 10 |
+
# -----------------------------
|
| 11 |
+
class HybridModel(nn.Module):
|
| 12 |
+
def __init__(self, input_size=1, hidden_size=64, num_layers=2):
|
| 13 |
+
super(HybridModel, self).__init__()
|
| 14 |
+
# LSTM sequence শেখার জন্য
|
| 15 |
+
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
|
| 16 |
+
# Transformer context শেখার জন্য
|
| 17 |
+
encoder_layer = nn.TransformerEncoderLayer(d_model=hidden_size, nhead=4)
|
| 18 |
+
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=2)
|
| 19 |
+
# Output layer
|
| 20 |
+
self.fc = nn.Linear(hidden_size, 1)
|
| 21 |
+
|
| 22 |
+
def forward(self, x):
|
| 23 |
+
lstm_out, _ = self.lstm(x)
|
| 24 |
+
trans_out = self.transformer(lstm_out)
|
| 25 |
+
out = self.fc(trans_out[:, -1, :])
|
| 26 |
+
return out
|
| 27 |
+
|
| 28 |
+
# Dummy model (শুধু demo এর জন্য)
|
| 29 |
+
model = HybridModel()
|
| 30 |
+
|
| 31 |
+
# -----------------------------
|
| 32 |
+
# Prediction Function
|
| 33 |
+
# -----------------------------
|
| 34 |
+
def predict(input_value):
|
| 35 |
+
try:
|
| 36 |
+
val = float(input_value)
|
| 37 |
+
x = torch.tensor([[[val]]], dtype=torch.float32)
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
pred = model(x).item()
|
| 40 |
+
return f"🔮 Prediction: {pred:.2f}"
|
| 41 |
+
except:
|
| 42 |
+
return "❌ Invalid input! Please enter a number like 2.1"
|
| 43 |
+
|
| 44 |
+
# -----------------------------
|
| 45 |
+
# Gradio UI
|
| 46 |
+
# -----------------------------
|
| 47 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 48 |
+
gr.Markdown("## 🟣 Real-Time Hybrid AI Prediction System\nবাংলায় সহজ UI, Dark Mode")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
input_box = gr.Textbox(label="Live Data Input (e.g. 2.1)", placeholder="Type number ➕ ⬜ ➖ ▫️ ➕ ⬜ ➖")
|
| 52 |
+
output_box = gr.Textbox(label="Prediction Result", interactive=False)
|
| 53 |
+
|
| 54 |
+
input_box.change(fn=predict, inputs=input_box, outputs=output_box)
|
| 55 |
+
|
| 56 |
+
# CSV Upload Option
|
| 57 |
+
csv_file = gr.File(label="Upload CSV for Initial Learning", file_types=[".csv"])
|
| 58 |
+
|
| 59 |
+
demo.launch()
|