Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
-
# π Load Model (CPU
|
| 8 |
# ----------------------------
|
| 9 |
-
@gr.cache
|
| 10 |
def load_model():
|
| 11 |
-
|
| 12 |
-
tokenizer =
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
)
|
| 17 |
-
|
| 18 |
return model, tokenizer
|
| 19 |
|
| 20 |
print("Loading model...")
|
|
@@ -30,16 +30,14 @@ def generate_training_program(
|
|
| 30 |
temperature=0.7,
|
| 31 |
top_p=0.9
|
| 32 |
):
|
| 33 |
-
"""
|
| 34 |
-
|
| 35 |
-
prompt_text = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
| 36 |
|
| 37 |
### Instruction:
|
| 38 |
{instruction}
|
| 39 |
-
|
| 40 |
### Response:
|
| 41 |
"""
|
| 42 |
-
inputs = tokenizer(prompt_text, return_tensors="pt")
|
| 43 |
|
| 44 |
start_time = time.time()
|
| 45 |
outputs = model.generate(
|
|
@@ -52,9 +50,9 @@ def generate_training_program(
|
|
| 52 |
)
|
| 53 |
generation_time = time.time() - start_time
|
| 54 |
|
| 55 |
-
generated_text = tokenizer.
|
| 56 |
-
|
| 57 |
-
# Extract
|
| 58 |
if "### Response:" in generated_text:
|
| 59 |
response = generated_text.split("### Response:")[-1].strip()
|
| 60 |
else:
|
|
@@ -66,7 +64,9 @@ def generate_training_program(
|
|
| 66 |
# π¨ Gradio Interface
|
| 67 |
# ----------------------------
|
| 68 |
custom_css = """
|
| 69 |
-
.gradio-container {
|
|
|
|
|
|
|
| 70 |
.main-header {
|
| 71 |
text-align: center;
|
| 72 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
@@ -86,14 +86,10 @@ examples = [
|
|
| 86 |
]
|
| 87 |
|
| 88 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 89 |
-
|
| 90 |
-
# Header
|
| 91 |
gr.HTML("""
|
| 92 |
<div class="main-header">
|
| 93 |
<h1>π― AI Training Program Generator</h1>
|
| 94 |
-
<p
|
| 95 |
-
Generate comprehensive, professional training programs instantly using AI
|
| 96 |
-
</p>
|
| 97 |
</div>
|
| 98 |
""")
|
| 99 |
|
|
@@ -101,53 +97,22 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 101 |
with gr.Column(scale=1):
|
| 102 |
instruction_input = gr.Textbox(
|
| 103 |
label="π Training Program Description",
|
| 104 |
-
placeholder="
|
| 105 |
lines=5
|
| 106 |
)
|
| 107 |
|
| 108 |
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 109 |
-
max_tokens_slider = gr.Slider(
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
label="Max Output Length",
|
| 115 |
-
info="Longer programs take more time on CPU"
|
| 116 |
-
)
|
| 117 |
-
temperature_slider = gr.Slider(
|
| 118 |
-
minimum=0.1,
|
| 119 |
-
maximum=1.5,
|
| 120 |
-
value=0.7,
|
| 121 |
-
step=0.1,
|
| 122 |
-
label="Creativity (Temperature)"
|
| 123 |
-
)
|
| 124 |
-
top_p_slider = gr.Slider(
|
| 125 |
-
minimum=0.5,
|
| 126 |
-
maximum=1.0,
|
| 127 |
-
value=0.9,
|
| 128 |
-
step=0.05,
|
| 129 |
-
label="Diversity (Top-p)"
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
generate_btn = gr.Button("π Generate Training Program", variant="primary", size="lg")
|
| 133 |
|
| 134 |
with gr.Column(scale=1):
|
| 135 |
-
output_text = gr.Textbox(
|
| 136 |
-
|
| 137 |
-
lines=25,
|
| 138 |
-
show_copy_button=True
|
| 139 |
-
)
|
| 140 |
-
generation_info = gr.Textbox(
|
| 141 |
-
label="βΉοΈ Generation Info",
|
| 142 |
-
interactive=False,
|
| 143 |
-
show_label=False
|
| 144 |
-
)
|
| 145 |
|
| 146 |
-
gr.Examples(
|
| 147 |
-
examples=examples,
|
| 148 |
-
inputs=[instruction_input],
|
| 149 |
-
label="π‘ Quick Start Examples"
|
| 150 |
-
)
|
| 151 |
|
| 152 |
generate_btn.click(
|
| 153 |
fn=generate_training_program,
|
|
@@ -155,7 +120,13 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 155 |
outputs=[output_text, generation_info]
|
| 156 |
)
|
| 157 |
|
| 158 |
-
#
|
|
|
|
|
|
|
| 159 |
if __name__ == "__main__":
|
| 160 |
-
demo.queue(max_size=
|
| 161 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from unsloth import FastLanguageModel
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
+
# π Load Model (CPU)
|
| 8 |
# ----------------------------
|
|
|
|
| 9 |
def load_model():
|
| 10 |
+
max_seq_length = 1024
|
| 11 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 12 |
+
model_name="umarfarzan/my-finetuned-model2-lora",
|
| 13 |
+
max_seq_length=max_seq_length,
|
| 14 |
+
dtype=None,
|
| 15 |
+
load_in_4bit=True # 4-bit works on CPU too
|
| 16 |
)
|
| 17 |
+
FastLanguageModel.for_inference(model)
|
| 18 |
return model, tokenizer
|
| 19 |
|
| 20 |
print("Loading model...")
|
|
|
|
| 30 |
temperature=0.7,
|
| 31 |
top_p=0.9
|
| 32 |
):
|
| 33 |
+
prompt_text = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
|
|
|
|
|
|
| 34 |
|
| 35 |
### Instruction:
|
| 36 |
{instruction}
|
| 37 |
+
### Input:
|
| 38 |
### Response:
|
| 39 |
"""
|
| 40 |
+
inputs = tokenizer([prompt_text], return_tensors="pt").to("cpu")
|
| 41 |
|
| 42 |
start_time = time.time()
|
| 43 |
outputs = model.generate(
|
|
|
|
| 50 |
)
|
| 51 |
generation_time = time.time() - start_time
|
| 52 |
|
| 53 |
+
generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 54 |
+
|
| 55 |
+
# Extract only the response part
|
| 56 |
if "### Response:" in generated_text:
|
| 57 |
response = generated_text.split("### Response:")[-1].strip()
|
| 58 |
else:
|
|
|
|
| 64 |
# π¨ Gradio Interface
|
| 65 |
# ----------------------------
|
| 66 |
custom_css = """
|
| 67 |
+
.gradio-container {
|
| 68 |
+
font-family: 'Inter', sans-serif;
|
| 69 |
+
}
|
| 70 |
.main-header {
|
| 71 |
text-align: center;
|
| 72 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
|
|
| 86 |
]
|
| 87 |
|
| 88 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
| 89 |
gr.HTML("""
|
| 90 |
<div class="main-header">
|
| 91 |
<h1>π― AI Training Program Generator</h1>
|
| 92 |
+
<p>Generate professional training programs instantly using AI</p>
|
|
|
|
|
|
|
| 93 |
</div>
|
| 94 |
""")
|
| 95 |
|
|
|
|
| 97 |
with gr.Column(scale=1):
|
| 98 |
instruction_input = gr.Textbox(
|
| 99 |
label="π Training Program Description",
|
| 100 |
+
placeholder="Describe your training program...",
|
| 101 |
lines=5
|
| 102 |
)
|
| 103 |
|
| 104 |
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 105 |
+
max_tokens_slider = gr.Slider(500, 2000, value=500, step=100, label="Max Output Length")
|
| 106 |
+
temperature_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Creativity (Temperature)")
|
| 107 |
+
top_p_slider = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Diversity (Top-p)")
|
| 108 |
+
|
| 109 |
+
generate_btn = gr.Button("π Generate Training Program", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
with gr.Column(scale=1):
|
| 112 |
+
output_text = gr.Textbox(label="π Generated Training Program", lines=25, show_copy_button=True)
|
| 113 |
+
generation_info = gr.Textbox(label="βΉοΈ Generation Info", interactive=False, show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
gr.Examples(examples=examples, inputs=[instruction_input], label="π‘ Example Prompts")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
generate_btn.click(
|
| 118 |
fn=generate_training_program,
|
|
|
|
| 120 |
outputs=[output_text, generation_info]
|
| 121 |
)
|
| 122 |
|
| 123 |
+
# ----------------------------
|
| 124 |
+
# π Launch App
|
| 125 |
+
# ----------------------------
|
| 126 |
if __name__ == "__main__":
|
| 127 |
+
demo.queue(max_size=10)
|
| 128 |
+
demo.launch(
|
| 129 |
+
share=True,
|
| 130 |
+
server_name="0.0.0.0",
|
| 131 |
+
server_port=7860
|
| 132 |
+
)
|