Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from unsloth import FastLanguageModel
|
3 |
+
from transformers import TextStreamer
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load the model and tokenizer
|
7 |
+
model_name = "BidhanAcharya/FineTunedQWENoncoding" # Replace with your actual model path
|
8 |
+
max_seq_length = 512 # Example, adjust according to your model
|
9 |
+
dtype = torch.float16 # Adjust if necessary (use torch.float32 for CPU)
|
10 |
+
load_in_4bit = True # If needed, set to False if not using 4-bit precision
|
11 |
+
|
12 |
+
# Load the model and tokenizer with the FastLanguageModel method
|
13 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
14 |
+
model_name=model_name,
|
15 |
+
max_seq_length=max_seq_length,
|
16 |
+
dtype=dtype,
|
17 |
+
load_in_4bit=load_in_4bit
|
18 |
+
)
|
19 |
+
|
20 |
+
# Set the model to inference mode
|
21 |
+
FastLanguageModel.for_inference(model)
|
22 |
+
|
23 |
+
# Define the Alpaca prompt format
|
24 |
+
alpaca_prompt = "### Instruction:\n{}\n\n### Input:\n{}\n\n### Response:\n{}"
|
25 |
+
|
26 |
+
# Gradio function for performing inference
|
27 |
+
def generate_response(instruction, input_data):
|
28 |
+
# Handle case where input data is empty
|
29 |
+
if input_data.strip() == "":
|
30 |
+
input_data = "No additional input provided."
|
31 |
+
|
32 |
+
# Format the prompt using the instruction and input data
|
33 |
+
inputs = tokenizer(
|
34 |
+
[
|
35 |
+
alpaca_prompt.format(
|
36 |
+
instruction, # user-provided instruction
|
37 |
+
input_data, # optional user input data
|
38 |
+
"" # output (leave blank for generation)
|
39 |
+
)
|
40 |
+
],
|
41 |
+
return_tensors="pt"
|
42 |
+
)
|
43 |
+
|
44 |
+
# Move input tensors to the correct device (GPU/CPU)
|
45 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
46 |
+
inputs = inputs.to(device)
|
47 |
+
|
48 |
+
# Generate tokens with the model
|
49 |
+
generated_tokens = model.generate(**inputs, max_new_tokens=500)
|
50 |
+
|
51 |
+
# Decode the generated tokens into text
|
52 |
+
generated_text = tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
|
53 |
+
|
54 |
+
return generated_text
|
55 |
+
|
56 |
+
# Gradio Interface
|
57 |
+
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown("# FastLanguageModel Inference App")
|
59 |
+
|
60 |
+
instruction_input = gr.Textbox(label="Instruction", placeholder="Enter your instruction here")
|
61 |
+
input_data_input = gr.Textbox(label="Input Data (Optional)", placeholder="Enter your input data here (optional)")
|
62 |
+
output_text = gr.Textbox(label="Generated Response")
|
63 |
+
|
64 |
+
generate_button = gr.Button("Generate Response")
|
65 |
+
|
66 |
+
# Connect the Gradio button click event to the response generation function
|
67 |
+
generate_button.click(
|
68 |
+
fn=generate_response,
|
69 |
+
inputs=[instruction_input, input_data_input],
|
70 |
+
outputs=[output_text]
|
71 |
+
)
|
72 |
+
|
73 |
+
# Launch the Gradio app
|
74 |
+
demo.launch()
|