Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
bnb_4bit_use_double_quant=True,
|
| 11 |
-
bnb_4bit_quant_type="nf4"
|
| 12 |
)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
low_cpu_mem_usage=True,
|
| 20 |
-
trust_remote_code=True
|
| 21 |
-
)
|
| 22 |
-
model.eval()
|
| 23 |
-
|
| 24 |
-
def generate_code(prompt):
|
| 25 |
-
formatted = f"### Instruction:\n{prompt}\n### Response:\n"
|
| 26 |
-
|
| 27 |
-
inputs = tokenizer(
|
| 28 |
-
formatted,
|
| 29 |
-
return_tensors="pt",
|
| 30 |
-
truncation=True,
|
| 31 |
-
max_length=1024
|
| 32 |
)
|
| 33 |
-
|
| 34 |
-
input_ids = inputs["input_ids"].to(model.device)
|
| 35 |
-
attention_mask = inputs["attention_mask"].to(model.device)
|
| 36 |
-
|
| 37 |
-
with torch.no_grad():
|
| 38 |
-
output = model.generate(
|
| 39 |
-
input_ids=input_ids,
|
| 40 |
-
attention_mask=attention_mask,
|
| 41 |
-
max_new_tokens=512,
|
| 42 |
-
do_sample=True,
|
| 43 |
-
temperature=0.7,
|
| 44 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 45 |
-
eos_token_id=tokenizer.eos_token_id
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
new_tokens = output[0][input_ids.shape[-1]:]
|
| 49 |
-
result = tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 50 |
-
return result.strip()
|
| 51 |
|
| 52 |
gr.Interface(
|
| 53 |
-
fn=
|
| 54 |
-
inputs=gr.Textbox(
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
),
|
| 59 |
-
outputs=gr.Code(label="Hasil kode", language="python"),
|
| 60 |
-
title="Coding Assistant - Uncensored",
|
| 61 |
-
description="Powered by Qwen3.5-4B Uncensored"
|
| 62 |
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Token dibaca dari secret, bukan hardcode
|
| 6 |
+
token = os.environ.get("HF_TOKEN")
|
| 7 |
|
| 8 |
+
client = InferenceClient(
|
| 9 |
+
model="NousResearch/Hermes-3-Llama-3.1-8B",
|
| 10 |
+
token=token
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
+
def generate(prompt):
|
| 14 |
+
response = client.chat_completion(
|
| 15 |
+
messages=[{"role": "user", "content": prompt}],
|
| 16 |
+
max_tokens=512,
|
| 17 |
+
temperature=0.7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
gr.Interface(
|
| 22 |
+
fn=generate,
|
| 23 |
+
inputs=gr.Textbox(lines=4, label="Tanya apa saja"),
|
| 24 |
+
outputs=gr.Textbox(label="Jawaban"),
|
| 25 |
+
title="AI Assistant - Uncensored",
|
| 26 |
+
description="Powered by HF Inference API"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
).launch()
|