GarGerry commited on
Commit
63a6540
·
verified ·
1 Parent(s): 6987059

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -17
app.py CHANGED
@@ -1,23 +1,52 @@
 
 
1
  import gradio as gr
2
- from transformers import pipeline, set_seed
3
 
4
- # Memuat model GPT-2 untuk generasi teks
5
- generator = pipeline('text-generation', model='gpt2')
 
 
 
 
 
 
6
 
7
- # Menetapkan seed untuk hasil yang konsisten
8
- set_seed(42)
9
 
10
- # Fungsi untuk menghasilkan respons chatbot
11
- def chatbot_response(prompt):
12
- response = generator(prompt, max_length=50, num_return_sequences=1)
13
- return response[0]['generated_text']
 
 
14
 
15
- # Membuat antarmuka Gradio untuk input dan output
16
- interface = gr.Interface(fn=chatbot_response,
17
- inputs="text",
18
- outputs="text",
19
- title="Chatbot with GPT-2",
20
- description="Masukkan teks Anda, dan chatbot akan memberikan respons berdasarkan model GPT-2.")
 
 
 
 
 
 
 
21
 
22
- # Meluncurkan aplikasi
23
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
  import gradio as gr
 
4
 
5
+ # Setup model dan tokenizer
6
+ torch.random.manual_seed(0)
7
+ model = AutoModelForCausalLM.from_pretrained(
8
+ "microsoft/Phi-3-mini-128k-instruct",
9
+ device_map="cuda", # Gunakan 'cpu' jika tidak menggunakan GPU
10
+ torch_dtype="auto",
11
+ trust_remote_code=True,
12
+ )
13
 
14
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct")
 
15
 
16
+ # Pipeline untuk text-generation
17
+ pipe = pipeline(
18
+ "text-generation",
19
+ model=model,
20
+ tokenizer=tokenizer,
21
+ )
22
 
23
+ # Fungsi untuk menghasilkan respons
24
+ def generate_response(input_text):
25
+ messages = [
26
+ {"role": "system", "content": "You are a helpful AI assistant."},
27
+ {"role": "user", "content": input_text}
28
+ ]
29
+
30
+ generation_args = {
31
+ "max_new_tokens": 500,
32
+ "return_full_text": False,
33
+ "temperature": 0.7, # Bisa disesuaikan untuk variasi output
34
+ "do_sample": True, # Mengaktifkan sampling untuk variasi output
35
+ }
36
 
37
+ output = pipe(messages, **generation_args)
38
+ return output[0]['generated_text']
39
+
40
+ # Membuat antarmuka menggunakan Gradio
41
+ iface = gr.Interface(
42
+ fn=generate_response, # Fungsi untuk menangani input
43
+ inputs=gr.Textbox(label="Ask me anything!", placeholder="Tanyakan sesuatu..."), # Input teks
44
+ outputs=gr.Textbox(label="AI Response"), # Output teks dari AI
45
+ title="AI Chatbot Assistant", # Judul aplikasi
46
+ description="Tanya apapun, saya siap membantu!", # Deskripsi aplikasi
47
+ theme="compact", # Tema antarmuka
48
+ layout="vertical", # Layout input dan output secara vertikal
49
+ )
50
+
51
+ # Menjalankan antarmuka
52
+ iface.launch()