piyushgrover commited on
Commit
2674eef
Β·
verified Β·
1 Parent(s): 7da381d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +56 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
+ from peft import PeftModel
5
+
6
+ # βœ… Model and Tokenizer Loading
7
+ model_name = "microsoft/phi-2"
8
+ #device_map = {"": 0}
9
+
10
+ # Load base model
11
+ base_model = AutoModelForCausalLM.from_pretrained(
12
+ model_name,
13
+ low_cpu_mem_usage=True,
14
+ return_dict=True,
15
+ torch_dtype=torch.float16,
16
+ trust_remote_code=True,
17
+ device_map="auto",
18
+ )
19
+
20
+ # Load fine-tuned LoRA weights
21
+ fine_tuned_model_path = "./phi2-qlora-adapter"
22
+ model = PeftModel.from_pretrained(base_model, fine_tuned_model_path)
23
+ model = model.merge_and_unload() # Merge LoRA weights
24
+
25
+ # Load tokenizer
26
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
27
+ tokenizer.pad_token = tokenizer.eos_token
28
+ tokenizer.padding_side = "right"
29
+
30
+ # βœ… Set up text generation pipeline
31
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=500)
32
+
33
+
34
+ # βœ… Chatbot Function
35
+ def chat(user_input, history=[]):
36
+ """Generates a response from the fine-tuned Phi-2 model."""
37
+ prompt = f"\n\n### User:\n{user_input}\n\n### Assistant:\n"
38
+ response = generator(prompt, max_length=500, do_sample=True)
39
+ answer = response[0]["generated_text"].split("### Assistant:\n")[-1].strip()
40
+
41
+ # Append conversation history
42
+ history.append((user_input, answer))
43
+ return "", history
44
+
45
+
46
+ # βœ… Create Gradio Chat Interface
47
+ chatbot = gr.ChatInterface(
48
+ fn=chat,
49
+ title="Fine-Tuned Phi-2 Chat Assistant",
50
+ description="πŸš€ Chat with a fine-tuned Phi-2 model. Ask anything!",
51
+ theme="compact",
52
+ )
53
+
54
+ # βœ… Launch App
55
+ if __name__ == "__main__":
56
+ chatbot.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio
4
+ peft
5
+ accelerate