Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,153 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
top_p,
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Check if CUDA is available
|
| 7 |
+
if torch.cuda.is_available():
|
| 8 |
+
print(f"Using GPU: {torch.cuda.get_device_name(0)}")
|
| 9 |
+
device = "cuda"
|
| 10 |
+
else:
|
| 11 |
+
print("GPU not available, using CPU")
|
| 12 |
+
device = "cpu"
|
| 13 |
|
| 14 |
+
# Model constants
|
| 15 |
+
MODEL_ID = "Ansah-AI/E1-4BIT-GGUF"
|
| 16 |
+
MODEL_REVISION = "main" # Change this if you need a specific revision
|
| 17 |
+
|
| 18 |
+
# Function to download and load the model and tokenizer
|
| 19 |
+
def load_model():
|
| 20 |
+
print(f"Loading model: {MODEL_ID}")
|
| 21 |
+
|
| 22 |
+
# Load model with 4-bit quantization
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
+
MODEL_ID,
|
| 25 |
+
revision=MODEL_REVISION,
|
| 26 |
+
device_map="auto",
|
| 27 |
+
load_in_4bit=True, # Enable 4-bit quantization
|
| 28 |
+
trust_remote_code=True
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Load tokenizer
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 33 |
+
|
| 34 |
+
print("Model and tokenizer loaded successfully!")
|
| 35 |
+
|
| 36 |
+
return model, tokenizer
|
| 37 |
|
| 38 |
+
# Function to generate text from the model
|
| 39 |
+
def generate_text(prompt, max_length=256, temperature=0.7, top_p=0.9, top_k=40):
|
| 40 |
+
# Ensure the model and tokenizer are loaded
|
| 41 |
+
global model, tokenizer
|
| 42 |
+
|
| 43 |
+
# Print generation parameters for debugging
|
| 44 |
+
print(f"Generating with parameters: max_length={max_length}, temp={temperature}, top_p={top_p}, top_k={top_k}")
|
| 45 |
+
|
| 46 |
+
# Create the text generation pipeline
|
| 47 |
+
text_generator = pipeline(
|
| 48 |
+
"text-generation",
|
| 49 |
+
model=model,
|
| 50 |
+
tokenizer=tokenizer,
|
| 51 |
+
device_map="auto"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Generate the text
|
| 55 |
+
generation_config = {
|
| 56 |
+
"max_length": max_length,
|
| 57 |
+
"temperature": temperature,
|
| 58 |
+
"top_p": top_p,
|
| 59 |
+
"top_k": top_k,
|
| 60 |
+
"num_return_sequences": 1,
|
| 61 |
+
"do_sample": temperature > 0.1, # Use sampling if temperature is significant
|
| 62 |
+
"pad_token_id": tokenizer.eos_token_id
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
result = text_generator(
|
| 67 |
+
prompt,
|
| 68 |
+
**generation_config
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Return the generated text
|
| 72 |
+
return result[0]["generated_text"]
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"Error generating text: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# Main function to create and run the Gradio interface
|
| 78 |
+
def main():
|
| 79 |
+
# Load the model and tokenizer
|
| 80 |
+
global model, tokenizer
|
| 81 |
+
model, tokenizer = load_model()
|
| 82 |
+
|
| 83 |
+
# Create the Gradio interface
|
| 84 |
+
with gr.Blocks(title="E1-4BIT-GGUF Model Interface") as demo:
|
| 85 |
+
gr.Markdown("# E1-4BIT-GGUF Model Interface")
|
| 86 |
+
gr.Markdown("Enter your prompt below to generate text using the Ansah-AI/E1-4BIT-GGUF model.")
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
with gr.Column(scale=4):
|
| 90 |
+
prompt_input = gr.Textbox(
|
| 91 |
+
label="Prompt",
|
| 92 |
+
placeholder="Enter your prompt here...",
|
| 93 |
+
lines=5
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
with gr.Column(scale=1):
|
| 97 |
+
max_length = gr.Slider(
|
| 98 |
+
minimum=64,
|
| 99 |
+
maximum=2048,
|
| 100 |
+
value=256,
|
| 101 |
+
step=32,
|
| 102 |
+
label="Max Length"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
temperature = gr.Slider(
|
| 106 |
+
minimum=0.1,
|
| 107 |
+
maximum=1.5,
|
| 108 |
+
value=0.7,
|
| 109 |
+
step=0.1,
|
| 110 |
+
label="Temperature"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
top_p = gr.Slider(
|
| 114 |
+
minimum=0.1,
|
| 115 |
+
maximum=1.0,
|
| 116 |
+
value=0.9,
|
| 117 |
+
step=0.05,
|
| 118 |
+
label="Top P"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
top_k = gr.Slider(
|
| 122 |
+
minimum=1,
|
| 123 |
+
maximum=100,
|
| 124 |
+
value=40,
|
| 125 |
+
step=1,
|
| 126 |
+
label="Top K"
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
generate_button = gr.Button("Generate")
|
| 130 |
+
output_text = gr.Textbox(label="Generated Text", lines=10)
|
| 131 |
+
|
| 132 |
+
# Set up the button click event
|
| 133 |
+
generate_button.click(
|
| 134 |
+
fn=generate_text,
|
| 135 |
+
inputs=[prompt_input, max_length, temperature, top_p, top_k],
|
| 136 |
+
outputs=output_text
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Add examples
|
| 140 |
+
gr.Examples(
|
| 141 |
+
examples=[
|
| 142 |
+
["Write a short story about a space explorer discovering a new planet."],
|
| 143 |
+
["Explain quantum computing to a high school student."],
|
| 144 |
+
["Create a recipe for a chocolate cake."]
|
| 145 |
+
],
|
| 146 |
+
inputs=prompt_input
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# Launch the interface
|
| 150 |
+
demo.launch(share=True) # share=True creates a public link
|
| 151 |
|
| 152 |
if __name__ == "__main__":
|
| 153 |
+
main()
|