|
|
import torch |
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
torch.random.manual_seed(0) |
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
|
"microsoft/Phi-3-mini-128k-instruct", |
|
|
device_map="cpu", |
|
|
torch_dtype="auto", |
|
|
trust_remote_code=True, |
|
|
attn_implementation="eager" |
|
|
) |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct") |
|
|
|
|
|
|
|
|
pipe = pipeline( |
|
|
"text-generation", |
|
|
model=model, |
|
|
tokenizer=tokenizer, |
|
|
) |
|
|
|
|
|
|
|
|
def generate_response(input_text): |
|
|
messages = [ |
|
|
{"role": "system", "content": "You are a helpful AI assistant."}, |
|
|
{"role": "user", "content": input_text} |
|
|
] |
|
|
|
|
|
generation_args = { |
|
|
"max_new_tokens": 500, |
|
|
"return_full_text": False, |
|
|
"temperature": 0.7, |
|
|
"do_sample": True, |
|
|
} |
|
|
|
|
|
output = pipe(messages, **generation_args) |
|
|
return output[0]['generated_text'] |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=generate_response, |
|
|
inputs=gr.Textbox(label="Ask me anything!", placeholder="Tanyakan sesuatu..."), |
|
|
outputs=gr.Textbox(label="AI Response"), |
|
|
title="AI Chatbot Assistant", |
|
|
description="Tanya apapun, saya siap membantu!", |
|
|
) |
|
|
|
|
|
|
|
|
iface.launch() |