Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,11 +8,40 @@ from functools import partial
|
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
import numpy as np
|
|
|
|
|
|
|
| 11 |
embedder = NVIDIAEmbeddings(model="nvolveqa_40k", model_type="query")
|
| 12 |
chat_model = ChatNVIDIA(model="llama2_13b") | StrOutputParser()
|
| 13 |
|
| 14 |
response_prompt = ChatPromptTemplate.from_messages([("system", "{system}"), ("user", "{input}")])
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def RPrint(preface=""):
|
| 17 |
def print_and_return(x, preface=""):
|
| 18 |
print(f"{preface}{x}")
|
|
|
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
import numpy as np
|
| 11 |
+
import keras
|
| 12 |
+
from keras import layers
|
| 13 |
embedder = NVIDIAEmbeddings(model="nvolveqa_40k", model_type="query")
|
| 14 |
chat_model = ChatNVIDIA(model="llama2_13b") | StrOutputParser()
|
| 15 |
|
| 16 |
response_prompt = ChatPromptTemplate.from_messages([("system", "{system}"), ("user", "{input}")])
|
| 17 |
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def train_model_neural_network(class0, class1):
|
| 21 |
+
## Classic deep learning training loop. If using this, train it to convergence
|
| 22 |
+
model = keras.Sequential([
|
| 23 |
+
layers.Dense(64, activation='tanh'),
|
| 24 |
+
layers.Dense(1, activation='sigmoid'),
|
| 25 |
+
])
|
| 26 |
+
## Since this network is so shallow and the embedding backbone is "kept frozen"
|
| 27 |
+
## a high learning rate should not overfit and will actually converge very quickly.
|
| 28 |
+
model.compile(
|
| 29 |
+
optimizer = keras.optimizers.Adam(learning_rate = 1),
|
| 30 |
+
loss = [keras.losses.BinaryCrossentropy(from_logits=False)],
|
| 31 |
+
metrics = [keras.metrics.BinaryAccuracy()],
|
| 32 |
+
)
|
| 33 |
+
## Since this uses stochastic gradient descent, we'll need to repeat this process
|
| 34 |
+
|
| 35 |
+
reps_per_batch = 64*5 ## <- repeat the dataset, effectively increasing "epochs" without printing too much
|
| 36 |
+
epochs = 2 ## <- one epoch should actually be sufficient; 2 to print out an updated training loss
|
| 37 |
+
x = np.array((class0 + class1) * reps_per_batch)
|
| 38 |
+
y = np.array(([0]*len(class0) + [1]*len(class1)) * reps_per_batch)
|
| 39 |
+
model.fit(x, y, epochs=epochs, batch_size=64, validation_split=.5)
|
| 40 |
+
return model
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
model1 = train_model_neural_network(poor_embeds, good_embeds)
|
| 44 |
+
|
| 45 |
def RPrint(preface=""):
|
| 46 |
def print_and_return(x, preface=""):
|
| 47 |
print(f"{preface}{x}")
|