1602sasa commited on
Commit
2b3024f
·
verified ·
1 Parent(s): 50e141e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn as nn
4
+ import numpy as np
5
+
6
+ print("Waking up BananaAI Developer 1 Mini...")
7
+
8
+ # 1. SET HARDWARE TO CPU (Hugging Face Free Tier)
9
+ device = torch.device('cpu')
10
+
11
+ # 2. REBUILD THE AI NEURONS EXACTLY AS THEY WERE
12
+ class BananaModel(nn.Module):
13
+ def __init__(self, vocab_size, hidden_size=256, num_layers=2):
14
+ super(BananaModel, self).__init__()
15
+ self.embed = nn.Embedding(vocab_size, hidden_size)
16
+ self.lstm = nn.LSTM(hidden_size, hidden_size, num_layers, batch_first=True, dropout=0.2)
17
+ self.fc = nn.Linear(hidden_size, vocab_size)
18
+
19
+ def forward(self, x, hidden):
20
+ out = self.embed(x)
21
+ out, hidden = self.lstm(out, hidden)
22
+ out = self.fc(out)
23
+ return out, hidden
24
+
25
+ # 3. LOAD THE BRAIN FILE
26
+ FILE_NAME = "banana_dev1_mini_brain.pth"
27
+ checkpoint = torch.load(FILE_NAME, map_location=device, weights_only=False)
28
+
29
+ vocab_size = checkpoint['vocab_size']
30
+ char_to_int = checkpoint['char_to_int']
31
+ int_to_char = {int(k): v for k, v in checkpoint['int_to_char'].items()}
32
+
33
+ model = BananaModel(vocab_size).to(device)
34
+ model.load_state_dict(checkpoint['model_state'])
35
+ model.eval() # Tell the AI it is time to chat, not train!
36
+
37
+ # 4. THE CHAT GENERATION LOGIC
38
+ def chat_banana(message, history):
39
+ # Format exactly how it learned in Kaggle
40
+ start_text = f"USER: {message}\nAI: "
41
+
42
+ # Ignore characters the AI has never seen before
43
+ chars = [char_to_int[c] for c in start_text if c in char_to_int]
44
+
45
+ if len(chars) == 0:
46
+ return "Error: Please use standard English/Python characters."
47
+
48
+ hidden = None
49
+
50
+ # Warm up the brain with the user's message
51
+ for c in chars:
52
+ x = torch.tensor([[c]]).to(device)
53
+ out, hidden = model(x, hidden)
54
+
55
+ x = torch.tensor([[chars[-1]]]).to(device)
56
+
57
+ generated_code = ""
58
+ max_length = 400 # It will generate up to 400 letters
59
+
60
+ for _ in range(max_length):
61
+ out, hidden = model(x, hidden)
62
+ prob = torch.nn.functional.softmax(out[0, -1], dim=0).detach().numpy()
63
+
64
+ # Pick the next letter based on AI logic!
65
+ char_ind = np.random.choice(vocab_size, p=prob)
66
+ char = int_to_char[char_ind]
67
+
68
+ generated_code += char
69
+ x = torch.tensor([[char_ind]]).to(device)
70
+
71
+ # If the AI tries to fake a conversation and starts typing "USER:", stop it!
72
+ if generated_code.endswith("USER:"):
73
+ generated_code = generated_code[:-5]
74
+ break
75
+
76
+ return generated_code.strip()
77
+
78
+ # 5. BUILD THE BEAUTIFUL OPENBANANA UI
79
+ demo = gr.ChatInterface(
80
+ fn=chat_banana,
81
+ title="🍌 BananaAI Developer 1 Mini",
82
+ description="**Company:** OpenBanana | **Developer:** Jan\n\nThis is a custom-built Generative AI trained from scratch on 1,000 Python scripts. It thinks character-by-character. Ask it to write code!",
83
+ theme="glass"
84
+ )
85
+
86
+ if __name__ == "__main__":
87
+ demo.launch()