Ahmed-El-Sharkawy commited on
Commit
c5ad68c
·
verified ·
1 Parent(s): 928b6fb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from unsloth import FastLanguageModel
4
+
5
+ # --- Load Model ---
6
+ max_seq_length = 4096
7
+ dtype = torch.float16
8
+ load_in_4bit = True
9
+
10
+ model, tokenizer = FastLanguageModel.from_pretrained(
11
+ model_name = "Ahmed-El-Sharkawy/Meta-Llama-3.1-8B-alpaca", # directly load your uploaded model
12
+ max_seq_length = max_seq_length,
13
+ dtype = dtype,
14
+ load_in_4bit = load_in_4bit,
15
+ )
16
+
17
+ FastLanguageModel.for_inference(model) # Enable 2x faster inference
18
+
19
+ # Define Alpaca prompt
20
+ alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
21
+ ### Instruction:
22
+ {instruction}
23
+ ### Input:
24
+ {input_text}
25
+ ### Response:
26
+ """
27
+
28
+ def generate_response(instruction, input_text):
29
+ prompt = alpaca_prompt.format(instruction=instruction, input_text=input_text)
30
+ inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
31
+
32
+ outputs = model.generate(
33
+ **inputs,
34
+ max_new_tokens=512,
35
+ use_cache=True
36
+ )
37
+
38
+ decoded_output = tokenizer.batch_decode(outputs, skip_special_tokens=True)
39
+ response = decoded_output[0].replace("<|begin_of_text|>", "").replace("<|end_of_text|>", "").strip()
40
+
41
+ # Optional: Remove the prompt part if model echoes it back
42
+ if prompt.strip() in response:
43
+ response = response.replace(prompt.strip(), "").strip()
44
+
45
+ return response
46
+
47
+ # --- Gradio UI ---
48
+ with gr.Blocks() as demo:
49
+ gr.Markdown("# 🌟 LLaMA-3 Alpaca Fine-tuned Chatbot")
50
+ with gr.Row():
51
+ instruction = gr.Textbox(label="Instruction", lines=2)
52
+ input_text = gr.Textbox(label="Input", lines=5)
53
+ output = gr.Textbox(label="Response", lines=10)
54
+ btn = gr.Button("Generate")
55
+ btn.click(generate_response, [instruction, input_text], output)
56
+
57
+ demo.launch()