Spaces:
Paused
Paused
Update app.py
Browse files9 unmodified lines101112131415161718192010 unmodified lines313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919223 unmodified lines1161171181191201219 unmodified lines# Initialize vLLM with CPU configurationvllm_model = LLM(model=model_name, tensor_parallel_size=1, device="cpu")# Load the OCR model and processorocr_model_name = "microsoft/trocr-small-handwritten"ocr_model = VisionEncoderDecoderModel.from_pretrained(ocr_model_name)ocr_processor = TrOCRProcessor.from_pretrained(ocr_model_name)#ocr_processor = AutoProcessor.from_pretrained(ocr_model_name)def generate_response(prompt, max_tokens, temperature, top_p):# Define sampling parameters10 unmodified linesgenerated_text = output[0].outputs[0].textreturn generated_textdef ocr_image(image_path):# Open the image from the file pathimage = Image.open(image_path).convert("RGB")# Preprocess the image for the OCR modelpixel_values = ocr_processor(images=image, return_tensors="pt").pixel_values# Perform OCR inferenceoutputs = ocr_model.generate(pixel_values)# Decode the generated tokens into texttext = ocr_processor.batch_decode(outputs, skip_special_tokens=True)[0]return text# Gradio UIwith gr.Blocks() as demo:gr.Markdown("# 🚀 Hugging Face Integration with vLLM and OCR (CPU)")gr.Markdown("Upload an image to extract text using OCR or generate text using the vLLM integration.")with gr.Tab("Text Generation"):with gr.Row():with gr.Column():prompt_input = gr.Textbox(label="Prompt",placeholder="Enter your prompt here...",lines=3,)max_tokens = gr.Slider(label="Max Tokens",minimum=10,maximum=500,value=100,step=10,)temperature = gr.Slider(label="Temperature",minimum=0.1,maximum=1.0,value=0.7,step=0.1,)top_p = gr.Slider(label="Top P",minimum=0.1,maximum=1.0,value=0.9,step=0.1,)submit_button = gr.Button("Generate")with gr.Column():output_text = gr.Textbox(label="Generated Text",lines=10,interactive=False,)submit_button.click(generate_response,23 unmodified linesinputs=[image_input],outputs=ocr_output,)# Launch the appdemo.launch()9 unmodified lines1011121314151610 unmodified lines272829303132333435363723 unmodified lines61626364656667686970719 unmodified lines# Initialize vLLM with CPU configurationvllm_model = LLM(model=model_name, tensor_parallel_size=1, device="cpu")def generate_response(prompt, max_tokens, temperature, top_p):# Define sampling parameters10 unmodified linesgenerated_text = output[0].outputs[0].textreturn generated_text# Gradio UIwith gr.Blocks() as demo:submit_button.click(generate_response,23 unmodified linesinputs=[image_input],outputs=ocr_output,)prompt =gr.Textbox()max_tokens = gr.Textbox()temperature = gr.Textbox()top_p = gr.Textbox()demo=gr.Interface(generate_response, inputs=[prompt, max_tokens,temperature, top_p], outputs="text")# Launch the appdemo.launch()
app.py+8-58