Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,20 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer
|
3 |
from vllm import LLM, SamplingParams
|
|
|
4 |
|
5 |
-
# Load the model and tokenizer from Hugging Face
|
6 |
model_name = "facebook/opt-125m"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
# Initialize vLLM with CPU configuration
|
10 |
vllm_model = LLM(model=model_name, tensor_parallel_size=1, device="cpu")
|
11 |
|
|
|
|
|
|
|
|
|
|
|
12 |
def generate_response(prompt, max_tokens, temperature, top_p):
|
13 |
# Define sampling parameters
|
14 |
sampling_params = SamplingParams(
|
@@ -24,53 +30,87 @@ def generate_response(prompt, max_tokens, temperature, top_p):
|
|
24 |
generated_text = output[0].outputs[0].text
|
25 |
return generated_text
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Gradio UI
|
28 |
with gr.Blocks() as demo:
|
29 |
-
gr.Markdown("# π Hugging Face Integration with vLLM (CPU)")
|
30 |
-
gr.Markdown("
|
31 |
|
32 |
-
with gr.
|
33 |
-
with gr.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# Launch the app
|
76 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoProcessor, VisionEncoderDecoderModel
|
3 |
from vllm import LLM, SamplingParams
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
# Load the language model and tokenizer from Hugging Face
|
7 |
model_name = "facebook/opt-125m"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
# Initialize vLLM with CPU configuration
|
11 |
vllm_model = LLM(model=model_name, tensor_parallel_size=1, device="cpu")
|
12 |
|
13 |
+
# Load the OCR model and processor
|
14 |
+
ocr_model_name = "microsoft/trocr-small-handwritten"
|
15 |
+
ocr_model = VisionEncoderDecoderModel.from_pretrained(ocr_model_name)
|
16 |
+
ocr_processor = AutoProcessor.from_pretrained(ocr_model_name)
|
17 |
+
|
18 |
def generate_response(prompt, max_tokens, temperature, top_p):
|
19 |
# Define sampling parameters
|
20 |
sampling_params = SamplingParams(
|
|
|
30 |
generated_text = output[0].outputs[0].text
|
31 |
return generated_text
|
32 |
|
33 |
+
def ocr_image(image):
|
34 |
+
# Open the image and preprocess for OCR
|
35 |
+
image = Image.open(image)
|
36 |
+
pixel_values = ocr_processor(images=image, return_tensors="pt").pixel_values
|
37 |
+
|
38 |
+
# Perform OCR
|
39 |
+
outputs = ocr_model.generate(pixel_values)
|
40 |
+
text = ocr_processor.decode(outputs[0], skip_special_tokens=True)
|
41 |
+
return text
|
42 |
+
|
43 |
# Gradio UI
|
44 |
with gr.Blocks() as demo:
|
45 |
+
gr.Markdown("# π Hugging Face Integration with vLLM and OCR (CPU)")
|
46 |
+
gr.Markdown("Upload an image to extract text using OCR or generate text using the vLLM integration.")
|
47 |
|
48 |
+
with gr.Tab("Text Generation"):
|
49 |
+
with gr.Row():
|
50 |
+
with gr.Column():
|
51 |
+
prompt_input = gr.Textbox(
|
52 |
+
label="Prompt",
|
53 |
+
placeholder="Enter your prompt here...",
|
54 |
+
lines=3,
|
55 |
+
)
|
56 |
+
max_tokens = gr.Slider(
|
57 |
+
label="Max Tokens",
|
58 |
+
minimum=10,
|
59 |
+
maximum=500,
|
60 |
+
value=100,
|
61 |
+
step=10,
|
62 |
+
)
|
63 |
+
temperature = gr.Slider(
|
64 |
+
label="Temperature",
|
65 |
+
minimum=0.1,
|
66 |
+
maximum=1.0,
|
67 |
+
value=0.7,
|
68 |
+
step=0.1,
|
69 |
+
)
|
70 |
+
top_p = gr.Slider(
|
71 |
+
label="Top P",
|
72 |
+
minimum=0.1,
|
73 |
+
maximum=1.0,
|
74 |
+
value=0.9,
|
75 |
+
step=0.1,
|
76 |
+
)
|
77 |
+
submit_button = gr.Button("Generate")
|
78 |
+
|
79 |
+
with gr.Column():
|
80 |
+
output_text = gr.Textbox(
|
81 |
+
label="Generated Text",
|
82 |
+
lines=10,
|
83 |
+
interactive=False,
|
84 |
+
)
|
85 |
|
86 |
+
submit_button.click(
|
87 |
+
generate_response,
|
88 |
+
inputs=[prompt_input, max_tokens, temperature, top_p],
|
89 |
+
outputs=output_text,
|
90 |
+
)
|
91 |
+
|
92 |
+
with gr.Tab("OCR"):
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column():
|
95 |
+
image_input = gr.Image(
|
96 |
+
label="Upload Image",
|
97 |
+
type="file",
|
98 |
+
image_mode="RGB",
|
99 |
+
)
|
100 |
+
ocr_submit_button = gr.Button("Extract Text")
|
101 |
+
|
102 |
+
with gr.Column():
|
103 |
+
ocr_output = gr.Textbox(
|
104 |
+
label="Extracted Text",
|
105 |
+
lines=10,
|
106 |
+
interactive=False,
|
107 |
+
)
|
108 |
+
|
109 |
+
ocr_submit_button.click(
|
110 |
+
ocr_image,
|
111 |
+
inputs=[image_input],
|
112 |
+
outputs=ocr_output,
|
113 |
+
)
|
114 |
|
115 |
# Launch the app
|
116 |
demo.launch()
|