GarGerry commited on
Commit
4777e40
·
verified ·
1 Parent(s): f065c86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -4
app.py CHANGED
@@ -5,18 +5,43 @@ import torch
5
  # Load Model dan Tokenizer
6
  model_name = "meta-llama/Llama-3.3-70B-Instruct"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
 
 
 
 
9
 
10
  # Fungsi Chatbot
11
  def chat(input_text):
12
  # Tokenisasi input
13
- inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
 
 
 
 
 
 
14
  # Hasilkan output menggunakan model
15
- outputs = model.generate(**inputs, max_length=1000, top_p=0.9, temperature=0.7)
 
 
 
 
 
 
 
16
  # Decode output menjadi teks
17
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
18
  return response
19
 
20
  # Membuat antarmuka Gradio
21
- iface = gr.Interface(fn=chat, inputs="text", outputs="text", live=True)
 
 
 
 
 
 
 
 
22
  iface.launch()
 
5
  # Load Model dan Tokenizer
6
  model_name = "meta-llama/Llama-3.3-70B-Instruct"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ device_map="auto",
11
+ torch_dtype=torch.float16
12
+ )
13
 
14
  # Fungsi Chatbot
15
  def chat(input_text):
16
  # Tokenisasi input
17
+ inputs = tokenizer(
18
+ input_text,
19
+ return_tensors="pt",
20
+ truncation=True, # Potong jika terlalu panjang
21
+ max_length=512 # Batasi panjang input
22
+ ).to("cuda")
23
+
24
  # Hasilkan output menggunakan model
25
+ outputs = model.generate(
26
+ **inputs,
27
+ max_length=200, # Panjang maksimal output
28
+ top_p=0.9, # Sampling berdasarkan probabilitas kumulatif
29
+ temperature=0.7, # Kontrol kreativitas output
30
+ num_return_sequences=1 # Jumlah respons yang dihasilkan
31
+ )
32
+
33
  # Decode output menjadi teks
34
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
35
  return response
36
 
37
  # Membuat antarmuka Gradio
38
+ iface = gr.Interface(
39
+ fn=chat,
40
+ inputs="text",
41
+ outputs="text",
42
+ title="Llama Chatbot",
43
+ description="Chatbot AI menggunakan model Llama-3.3-70B-Instruct."
44
+ )
45
+
46
+ # Meluncurkan Gradio
47
  iface.launch()