Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
from peft import PeftModel
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Directory where your fine-tuned Phi-2 model and associated files are stored.
|
7 |
+
# This directory should include files like:
|
8 |
+
# - adapter_config.json, adapter_model.safetensors,
|
9 |
+
# - tokenizer_config.json, tokenizer.json, merges.txt,
|
10 |
+
# - special_tokens_map.json, vocab.json, added_tokens.json, etc.
|
11 |
+
model_dir = "./phi2-finetune"
|
12 |
+
|
13 |
+
# Load the tokenizer.
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
15 |
+
|
16 |
+
# Load the base model. (Assumes the base model files are in model_dir.)
|
17 |
+
base_model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto")
|
18 |
+
|
19 |
+
# Load the adapter (PEFT) weights.
|
20 |
+
model = PeftModel.from_pretrained(base_model, model_dir)
|
21 |
+
|
22 |
+
def generate_response(prompt, max_new_tokens=200, temperature=0.7):
|
23 |
+
"""
|
24 |
+
Generate a response from the fine-tuned Phi-2 model given a prompt.
|
25 |
+
"""
|
26 |
+
# Tokenize the prompt and move tensors to the model's device.
|
27 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
28 |
+
|
29 |
+
# Generate output text using sampling.
|
30 |
+
outputs = model.generate(
|
31 |
+
**inputs,
|
32 |
+
max_new_tokens=max_new_tokens,
|
33 |
+
do_sample=True,
|
34 |
+
temperature=temperature
|
35 |
+
)
|
36 |
+
|
37 |
+
# Decode the generated tokens and return the response.
|
38 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
+
return response
|
40 |
+
|
41 |
+
# Create a Gradio interface with example prompts.
|
42 |
+
demo = gr.Interface(
|
43 |
+
fn=generate_response,
|
44 |
+
inputs=[
|
45 |
+
gr.Textbox(lines=4, label="Input Prompt"),
|
46 |
+
gr.Slider(50, 500, value=200, label="Max New Tokens"),
|
47 |
+
gr.Slider(0.0, 1.0, value=0.7, label="Temperature")
|
48 |
+
],
|
49 |
+
outputs=gr.Textbox(label="Response"),
|
50 |
+
title="Phi-2 Fine-tuned Chat",
|
51 |
+
description="A Hugging Face Space app serving the fine-tuned Phi-2 model trained on OpenAssistant/oasst1 data.",
|
52 |
+
examples=[
|
53 |
+
["Hello, how are you today?", 150, 0.7],
|
54 |
+
["Translate this sentence from English to French: I love programming.", 200, 0.8],
|
55 |
+
["Tell me a joke about artificial intelligence.", 180, 0.6]
|
56 |
+
]
|
57 |
+
)
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
demo.launch()
|