Adityak204 commited on
Commit
d37ca0c
·
1 Parent(s): 92863ce

Add initial implementation of Streamlit app for fine-tuned Phi-2 model demo with .gitignore and requirements

Browse files
Files changed (3) hide show
  1. .gitignore +66 -0
  2. app.py +120 -0
  3. requirements.txt +7 -0
.gitignore ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+
23
+ # Virtual Environment
24
+ venv/
25
+ env/
26
+ ENV/
27
+ .env
28
+ .venv
29
+
30
+ # IDE
31
+ .idea/
32
+ .vscode/
33
+ *.swp
34
+ *.swo
35
+ .project
36
+ .pydevproject
37
+ .settings
38
+
39
+ # Jupyter Notebook
40
+ .ipynb_checkpoints
41
+ *.ipynb_checkpoints/
42
+
43
+ # PyTorch
44
+ *.pth
45
+ *.pt
46
+ *.pkl
47
+
48
+ # Logs and databases
49
+ *.sqlite
50
+ *.db
51
+
52
+ # OS generated files
53
+ .DS_Store
54
+ .DS_Store?
55
+ ._*
56
+ .Spotlight-V100
57
+ .Trashes
58
+ ehthumbs.db
59
+ Thumbs.db
60
+
61
+ # Project specific
62
+ runs/
63
+ checkpoints/
64
+ outputs/
65
+ logs/
66
+ lightning_logs/
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
+ from peft import PeftModel
5
+ from huggingface_hub import snapshot_download
6
+
7
+ # Page configuration
8
+ st.set_page_config(page_title="Fine-tuned Phi-2 Demo", page_icon="🤖", layout="wide")
9
+
10
+ # Streamlit UI
11
+ st.title("Fine-tuned Phi-2 Model Demo")
12
+ st.markdown(
13
+ """
14
+ This app demonstrates a fine-tuned version of Microsoft's Phi-2 model.
15
+ Enter your prompt below and see the model generate text!
16
+ """
17
+ )
18
+
19
+
20
+ @st.cache_resource
21
+ def load_model():
22
+ with st.status("Loading model from Hugging Face...", expanded=True) as status:
23
+ st.write("Downloading checkpoint files...")
24
+ # Download the checkpoint files from your HF repo
25
+ model_repo = "Adityak204/phi2-finetuned-checkpoint"
26
+ checkpoint_path = snapshot_download(repo_id=model_repo)
27
+
28
+ st.write("Loading base model...")
29
+ # Load the base model
30
+ base_model = AutoModelForCausalLM.from_pretrained(
31
+ "microsoft/phi-2",
32
+ torch_dtype=torch.float16,
33
+ device_map="auto",
34
+ trust_remote_code=True,
35
+ )
36
+
37
+ st.write("Loading LoRA adapter weights...")
38
+ # Load the LoRA adapter weights
39
+ model = PeftModel.from_pretrained(
40
+ base_model, checkpoint_path, device_map="auto"
41
+ )
42
+
43
+ st.write("Loading tokenizer...")
44
+ # Load tokenizer
45
+ tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
46
+ tokenizer.pad_token = tokenizer.eos_token
47
+
48
+ st.write("Creating text generation pipeline...")
49
+ # Create text generation pipeline
50
+ pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer)
51
+
52
+ status.update(
53
+ label="Model loaded successfully!", state="complete", expanded=False
54
+ )
55
+
56
+ return pipe
57
+
58
+
59
+ # Load the model and pipeline (cached to avoid reloading)
60
+ pipe = load_model()
61
+
62
+ # Input form
63
+ with st.form("generation_form"):
64
+ prompt = st.text_area(
65
+ "Enter your prompt:",
66
+ height=150,
67
+ placeholder="Example: What is the square root of 81?",
68
+ )
69
+
70
+ max_length = st.slider(
71
+ "Maximum length:",
72
+ min_value=32,
73
+ max_value=1024,
74
+ value=256,
75
+ step=32,
76
+ help="Maximum number of tokens to generate",
77
+ )
78
+
79
+ generate_button = st.form_submit_button("Generate")
80
+
81
+ # Generate text when button is clicked
82
+ if generate_button and prompt:
83
+ with st.spinner("Generating..."):
84
+ # Generate text
85
+ prompt = f"""
86
+ ### prompter: {prompt}
87
+ ### assistant:
88
+ """
89
+ generated_text = pipe(
90
+ prompt,
91
+ max_length=max_length,
92
+ do_sample=True,
93
+ num_return_sequences=1,
94
+ )[0]["generated_text"]
95
+
96
+ # Display the result
97
+ st.subheader("Generated Output:")
98
+ st.write(generated_text)
99
+
100
+ # Copy button
101
+ st.text_area("Copy output:", value=generated_text, height=150)
102
+
103
+ # Sidebar with model information
104
+ with st.sidebar:
105
+ st.header("About This Model")
106
+ st.write(
107
+ """
108
+ This is a fine-tuned version of Microsoft's Phi-2 model.
109
+
110
+ The model was fine-tuned using QLoRA techniques to improve its performance on specific tasks.
111
+
112
+ Original model: [microsoft/phi-2](https://huggingface.co/microsoft/phi-2)
113
+
114
+ Fine-tuned model: [Adityak204/phi2-finetuned-checkpoint](https://huggingface.co/Adityak204/phi2-finetuned-checkpoint)
115
+ """
116
+ )
117
+
118
+ # Footer
119
+ st.markdown("---")
120
+ st.markdown("Powered by a fine-tuned version of Microsoft's Phi-2 model")
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit==1.28.0
2
+ torch>=2.0.0
3
+ transformers>=4.34.0
4
+ accelerate>=0.20.0
5
+ peft>=0.4.0
6
+ bitsandbytes>=0.40.0
7
+ huggingface-hub>=0.17.0