Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
# =============
|
3 |
+
# This is a complete app.py file for a text generation app using the Qwen/Qwen2.5-Coder-0.5B-Instruct-GGUF model.
|
4 |
+
# The app is built using Gradio and runs on a CPU without video memory.
|
5 |
+
|
6 |
+
# Imports
|
7 |
+
# =======
|
8 |
+
import gradio as gr
|
9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
10 |
+
import torch
|
11 |
+
|
12 |
+
# Constants
|
13 |
+
# =========
|
14 |
+
MODEL_NAME = "Qwen/Qwen2.5-Coder-0.5B-Instruct-GGUF"
|
15 |
+
DEVICE = "cpu" # Ensure the model runs on CPU
|
16 |
+
|
17 |
+
# Load Model and Tokenizer
|
18 |
+
# ========================
|
19 |
+
def load_model_and_tokenizer():
|
20 |
+
"""
|
21 |
+
Load the model and tokenizer from Hugging Face.
|
22 |
+
"""
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.bfloat16, device_map=DEVICE)
|
25 |
+
return tokenizer, model
|
26 |
+
|
27 |
+
tokenizer, model = load_model_and_tokenizer()
|
28 |
+
|
29 |
+
# Generate Text
|
30 |
+
# =============
|
31 |
+
def generate_text(prompt, max_length=100):
|
32 |
+
"""
|
33 |
+
Generate text based on the given prompt.
|
34 |
+
|
35 |
+
Args:
|
36 |
+
prompt (str): The input prompt for text generation.
|
37 |
+
max_length (int): The maximum length of the generated text.
|
38 |
+
|
39 |
+
Returns:
|
40 |
+
str: The generated text.
|
41 |
+
"""
|
42 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
|
43 |
+
outputs = model.generate(inputs.input_ids, max_length=max_length, num_return_sequences=1)
|
44 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
45 |
+
return generated_text
|
46 |
+
|
47 |
+
# Gradio Interface
|
48 |
+
# =================
|
49 |
+
def gradio_interface():
|
50 |
+
"""
|
51 |
+
Create and launch the Gradio interface.
|
52 |
+
"""
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=generate_text,
|
55 |
+
inputs=[
|
56 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter your prompt here..."),
|
57 |
+
gr.inputs.Slider(minimum=50, maximum=500, step=10, default=100, label="Max Length")
|
58 |
+
],
|
59 |
+
outputs="text",
|
60 |
+
title="Qwen2.5-Coder-0.5B-Instruct-GGUF Text Generation",
|
61 |
+
description="Generate text using the Qwen2.5-Coder-0.5B-Instruct-GGUF model."
|
62 |
+
)
|
63 |
+
iface.launch()
|
64 |
+
|
65 |
+
# Main
|
66 |
+
# ====
|
67 |
+
if __name__ == "__main__":
|
68 |
+
gradio_interface()
|
69 |
+
|
70 |
+
# Dependencies
|
71 |
+
# =============
|
72 |
+
# The following dependencies are required to run this app:
|
73 |
+
# - transformers
|
74 |
+
# - gradio
|
75 |
+
# - torch
|
76 |
+
#
|
77 |
+
# You can install these dependencies using pip:
|
78 |
+
# pip install transformers gradio torch
|