maryam1320 commited on
Commit
d62bb39
·
verified ·
1 Parent(s): adb553e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+
4
+ from gradientai import Gradient
5
+ import os
6
+ import pandas as pd
7
+
8
+ os.environ['GRADIENT_WORKSPACE_ID']='9d0447f2-fcd4-4177-9145-9f019fd59f1e_workspace'
9
+ os.environ['GRADIENT_ACCESS_TOKEN']='cPErsUMgadGMbzeq8z8W36eJn7UA0Uob'
10
+
11
+ df = pd.read_csv("https://raw.githubusercontent.com/CS-5302/CS-5302-Project-Group-15/main/Datasets/testing/combined_df.csv")
12
+ df
13
+
14
+ BATCH_SIZE = 100
15
+ NUM_EPOCHS = 1
16
+
17
+ def create_model_adapter(gradient):
18
+ base_model = gradient.get_base_model(base_model_slug="nous-hermes2")
19
+ new_model_adapter = base_model.create_model_adapter(
20
+ name="meta/llama-2-7b:73001d654114dad81ec65da3b834e2f691af1e1526453189b7bf36fb3f32d0f9"
21
+ )
22
+ print(f"Created model adapter with id {new_model_adapter.id}")
23
+ return new_model_adapter
24
+
25
+ def fine_tune_in_batches(df, gradient, batch_size, num_epochs):
26
+ new_model_adapter = create_model_adapter(gradient)
27
+
28
+ # Split the DataFrame into batches
29
+ batches = [df[i:i + batch_size] for i in range(0, len(df), batch_size)]
30
+
31
+ # Iterate over batches and perform fine-tuning
32
+ for batch_index, batch in enumerate(batches):
33
+ fine_tuning_samples = []
34
+ for _, row in batch.iterrows():
35
+ fine_tuning_samples.append({
36
+ "inputs": f"### Instruction: {row['prompts']}",
37
+ "targets": f"### Response: {row['results']}"
38
+ })
39
+
40
+ # Fine-tune for the given number of epochs
41
+ for epoch in range(num_epochs):
42
+ print(f"Fine-tuning batch {batch_index + 1} (epoch {epoch + 1})")
43
+ new_model_adapter.fine_tune(samples=fine_tuning_samples)
44
+
45
+ return new_model_adapter
46
+
47
+
48
+ def predict(prompt):
49
+ gradient = Gradient()
50
+ model_adapter = fine_tune_in_batches(df, gradient, BATCH_SIZE, NUM_EPOCHS)
51
+ sample_query = f"### Instruction: {prompt} \n\n### Response:"
52
+ completion = model_adapter.complete(query=sample_query, max_generated_token_count=100).generated_output
53
+ model_adapter.delete()
54
+ gradient.close()
55
+ return completion
56
+
57
+ interface = gr.Interface(fn=predict, inputs="text", outputs="text")
58
+
59
+ interface.launch()