Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,78 @@
|
|
1 |
-
|
2 |
-
import spaces
|
3 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
print(zero.device) # <-- 'cuda:0' 🤗
|
11 |
-
return f"Hello {zero + n} Tensor"
|
12 |
|
13 |
-
demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
|
14 |
-
demo.launch()
|
|
|
1 |
+
from unsloth import FastLanguageModel
|
|
|
2 |
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
|
6 |
+
dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
|
7 |
+
load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
|
8 |
+
alpaca_prompt = """Berikut adalah instruksi yang deskripsikan tugas dan sepasang input dan konteksnya. Tulis response sesuai dengan permintaan.
|
9 |
+
|
10 |
+
### Instruction:
|
11 |
+
{}
|
12 |
+
|
13 |
+
### Input:
|
14 |
+
{}
|
15 |
+
|
16 |
+
### Response:
|
17 |
+
{}"""
|
18 |
+
|
19 |
+
if True:
|
20 |
+
from unsloth import FastLanguageModel
|
21 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
22 |
+
model_name = "abdfajar707/llama3_8B_lora_model_rkp_v2", # YOUR MODEL YOU USED FOR TRAINING
|
23 |
+
max_seq_length = max_seq_length,
|
24 |
+
dtype = dtype,
|
25 |
+
load_in_4bit = load_in_4bit,
|
26 |
+
)
|
27 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
# Fungsi untuk menghasilkan respons
|
32 |
+
def generate_response(prompt, max_length=1000):
|
33 |
+
inputs = tokenizer(
|
34 |
+
[
|
35 |
+
alpaca_prompt.format(
|
36 |
+
prompt, # instruction
|
37 |
+
"", # input
|
38 |
+
"", # output - leave this blank for generation!
|
39 |
+
)
|
40 |
+
], return_tensors = "pt").to("cuda")
|
41 |
+
outputs = model.generate(**inputs, max_length=max_length, pad_token_id=tokenizer.eos_token_id)
|
42 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
43 |
+
return response
|
44 |
+
|
45 |
+
# Fungsi untuk antarmuka Gradio
|
46 |
+
def chatbot_interface(user_input, history):
|
47 |
+
# Buat respons dari model
|
48 |
+
response = generate_response(user_input)
|
49 |
+
# Perbarui riwayat percakapan
|
50 |
+
history.append(("User", user_input))
|
51 |
+
history.append(("Bot", response))
|
52 |
+
return history, history
|
53 |
+
|
54 |
+
# Definisikan input dan output untuk antarmuka menggunakan Gradio versi terbaru
|
55 |
+
inputs = [
|
56 |
+
gr.Textbox(lines=1, label="Masukkan pesan Anda"),
|
57 |
+
gr.State(value=[]) # Untuk menyimpan riwayat percakapan
|
58 |
+
]
|
59 |
+
|
60 |
+
outputs = [
|
61 |
+
gr.Chatbot(label="Respons Chatbot"),
|
62 |
+
gr.State() # Untuk memperbarui riwayat percakapan
|
63 |
+
]
|
64 |
|
65 |
+
# Buat dan luncurkan antarmuka Gradio
|
66 |
+
interface = gr.Interface(
|
67 |
+
fn=chatbot_interface,
|
68 |
+
inputs=inputs,
|
69 |
+
outputs=outputs,
|
70 |
+
title="LLaMA3 LoRA Chatbot",
|
71 |
+
description="Chatbot yang didukung oleh model LLaMA3 dengan modifikasi LoRA."
|
72 |
+
)
|
73 |
|
74 |
+
# Jalankan antarmuka
|
75 |
+
interface.launch()
|
|
|
|
|
76 |
|
77 |
+
#demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
|
78 |
+
#demo.launch()
|