Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
import torch
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
from peft import AutoPeftModelForCausalLM
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
|
| 8 |
+
|
| 9 |
+
def format_instruction(report):
|
| 10 |
+
return """### Instruction:
|
| 11 |
+
Classify the student into Placed/NotPlaced based on his/her college report details. The report includes marks scored by the student in various courses and extra curricular activities taken by them.
|
| 12 |
+
|
| 13 |
+
### Report:
|
| 14 |
+
{report}
|
| 15 |
+
|
| 16 |
+
### Label:
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
def postprocess(outputs, tokenizer, prompt):
|
| 20 |
+
outputs = outputs.numpy()
|
| 21 |
+
outputs = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 22 |
+
output = outputs[0][len(prompt):]
|
| 23 |
+
|
| 24 |
+
return output
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def run_model(report):
|
| 28 |
+
# load dataset and select a random sample
|
| 29 |
+
prompt = format_instruction(report)
|
| 30 |
+
|
| 31 |
+
# load base LLM model, LoRA params and tokenizer
|
| 32 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
| 33 |
+
Model_Repo_ID,
|
| 34 |
+
low_cpu_mem_usage=True,
|
| 35 |
+
torch_dtype=torch.float16,
|
| 36 |
+
load_in_4bit=True,
|
| 37 |
+
)
|
| 38 |
+
tokenizer = AutoTokenizer.from_pretrained(Model_Repo_ID)
|
| 39 |
+
input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids.cpu()
|
| 40 |
+
|
| 41 |
+
# inference
|
| 42 |
+
with torch.inference_mode():
|
| 43 |
+
outputs = model.generate(
|
| 44 |
+
input_ids=input_ids,
|
| 45 |
+
max_new_tokens=800,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
top_p=0.9,
|
| 48 |
+
temperature=0.9
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
return postprocess(outputs, tokenizer, report)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
iface = gr.Interface(fn=run_model, students_report="text", Status="text")
|
| 55 |
iface.launch()
|