Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,23 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
device_map="auto",
|
| 11 |
-
torch_dtype=torch.float16
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
title="Llama Chatbot",
|
| 43 |
-
description="Chatbot AI menggunakan model Llama-3.3-70B-Instruct."
|
| 44 |
)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
| 2 |
+
import os
|
|
|
|
| 3 |
|
| 4 |
+
# Ambil token dari Repository secrets
|
| 5 |
+
api_token = os.getenv("LLM")
|
| 6 |
+
|
| 7 |
+
# Inisialisasi Inference Client
|
| 8 |
+
client = InferenceClient(api_key=api_token)
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Pesan dari user
|
| 11 |
+
messages = [
|
| 12 |
+
{"role": "user", "content": "What is the capital of France?"}
|
| 13 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Inferensi menggunakan model
|
| 16 |
+
completion = client.chat.completions.create(
|
| 17 |
+
model="meta-llama/Llama-3.3-70B-Instruct",
|
| 18 |
+
messages=messages,
|
| 19 |
+
max_tokens=500
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Cetak jawaban dari model
|
| 23 |
+
print(completion.choices[0].message)
|