Download app.py from NowLoadY/ocr-gpt: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/spaces/NowLoadY/ocr-gpt/resolve/main/app.py
- Command line
-
hf download hf://spaces/NowLoadY/ocr-gpt/app.py
-
curl -L -o app.py https://huggingface.co/spaces/NowLoadY/ocr-gpt/resolve/main/app.py
1.4 kB
| import easyocr | |
| import gradio as gr | |
| import openai | |
| import requests | |
| # Set OpenAI API key | |
| openai.api_key = "your_api_key_here" | |
| # Initialize OCR reader | |
| reader = easyocr.Reader(['ch_sim', 'en'],gpu=False) | |
| # Define the OCR function | |
| # Update the ocr_gpt function to accept the API key as an input | |
| def ocr_gpt(image, gpt_opinion_prompt, api_key): | |
| openai.api_key = api_key | |
| ocr_result = reader.readtext(image) | |
| prompt = "Correct the following OCR result: " + ocr_result[0][1] | |
| response = openai.Completion.create(engine="davinci-codex", prompt=prompt, max_tokens=50, n=1, stop=None, temperature=0.5) | |
| corrected_text = response.choices[0].text.strip() | |
| gpt_opinion_response = openai.Completion.create(engine="davinci-codex", prompt=gpt_opinion_prompt, max_tokens=50, n=1, stop=None, temperature=0.5) | |
| gpt_opinion = gpt_opinion_response.choices[0].text.strip() | |
| return corrected_text, gpt_opinion | |
| # Define Gradio UI components | |
| image_input = gr.inputs.Image() | |
| gpt_input = gr.inputs.Textbox(lines=1, label="GPT Opinion Prompt") | |
| api_key_input = gr.inputs.Textbox(lines=1, label="API Key") | |
| ocr_output = gr.outputs.Textbox(label="OCR Result (GPT Corrected)") | |
| gpt_opinion_output = gr.outputs.Textbox(label="GPT Opinion on Image Information") | |
| # Create Gradio interface | |
| iface = gr.Interface(fn=ocr_gpt, inputs=[image_input, gpt_input, api_key_input], outputs=[ocr_output, gpt_opinion_output]) |