Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
4 |
+
from peft import PeftModel, PeftConfig
|
5 |
+
|
6 |
+
# Load the fine-tuned model and tokenizer
|
7 |
+
model_name = "your-username/phi-1_5-finetuned-oasst1" # Replace with your actual model path
|
8 |
+
config = PeftConfig.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, torch_dtype=torch.float16, device_map="auto")
|
10 |
+
model = PeftModel.from_pretrained(model, model_name)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
|
13 |
+
# Create a text generation pipeline
|
14 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device_map="auto")
|
15 |
+
|
16 |
+
def generate_text(prompt, max_length=100, temperature=0.7, top_p=0.9):
|
17 |
+
"""Generate text based on the input prompt."""
|
18 |
+
generated = generator(prompt, max_length=max_length, do_sample=True, temperature=temperature, top_p=top_p)
|
19 |
+
return generated[0]['generated_text']
|
20 |
+
|
21 |
+
# Custom CSS for styling
|
22 |
+
custom_css = """
|
23 |
+
body {
|
24 |
+
font-family: 'Arial', sans-serif;
|
25 |
+
background-color: #1a1a1a;
|
26 |
+
color: #ffffff;
|
27 |
+
}
|
28 |
+
.container {
|
29 |
+
max-width: 800px;
|
30 |
+
margin: auto;
|
31 |
+
padding: 20px;
|
32 |
+
background-color: #2a2a2a;
|
33 |
+
border-radius: 10px;
|
34 |
+
box-shadow: 0 0 10px rgba(255,255,255,0.1);
|
35 |
+
}
|
36 |
+
h1 {
|
37 |
+
color: #4a90e2;
|
38 |
+
text-align: center;
|
39 |
+
font-size: 2.5em;
|
40 |
+
margin-bottom: 20px;
|
41 |
+
}
|
42 |
+
.gr-input, .gr-box {
|
43 |
+
background-color: #3a3a3a !important;
|
44 |
+
border: 1px solid #4a4a4a !important;
|
45 |
+
color: #ffffff !important;
|
46 |
+
}
|
47 |
+
.gr-button {
|
48 |
+
background-color: #4a90e2 !important;
|
49 |
+
border: none !important;
|
50 |
+
color: white !important;
|
51 |
+
}
|
52 |
+
.gr-button:hover {
|
53 |
+
background-color: #3a80d2 !important;
|
54 |
+
}
|
55 |
+
.gr-form {
|
56 |
+
border: 1px solid #4a4a4a;
|
57 |
+
padding: 20px;
|
58 |
+
border-radius: 5px;
|
59 |
+
background-color: #2a2a2a;
|
60 |
+
}
|
61 |
+
.footer {
|
62 |
+
text-align: center;
|
63 |
+
margin-top: 20px;
|
64 |
+
font-size: 0.9em;
|
65 |
+
color: #888;
|
66 |
+
}
|
67 |
+
"""
|
68 |
+
|
69 |
+
# Create the Gradio interface
|
70 |
+
with gr.Blocks(css=custom_css) as iface:
|
71 |
+
gr.HTML("<div class='container'>")
|
72 |
+
gr.HTML("<h1>🤖 Phi-1.5 Fine-tuned Text Generator</h1>")
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
with gr.Column():
|
76 |
+
input_text = gr.Textbox(lines=5, label="Enter your prompt")
|
77 |
+
max_length = gr.Slider(minimum=50, maximum=500, value=100, step=10, label="Max Length")
|
78 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
|
79 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Top P")
|
80 |
+
generate_button = gr.Button("Generate Text", variant="primary")
|
81 |
+
|
82 |
+
with gr.Column():
|
83 |
+
output_text = gr.Textbox(lines=10, label="Generated Text")
|
84 |
+
|
85 |
+
generate_button.click(
|
86 |
+
generate_text,
|
87 |
+
inputs=[input_text, max_length, temperature, top_p],
|
88 |
+
outputs=output_text
|
89 |
+
)
|
90 |
+
|
91 |
+
gr.HTML("<div class='footer'>Powered by Hugging Face and Gradio</div>")
|
92 |
+
gr.HTML("</div>")
|
93 |
+
|
94 |
+
# Launch the app
|
95 |
+
iface.launch()
|