File size: 1,072 Bytes
ac38c2b
 
 
 
 
 
 
 
 
 
 
 
2ae3596
ac38c2b
 
2ae3596
 
ac38c2b
 
 
 
 
 
 
 
8050d40
2ae3596
ac38c2b
 
 
 
 
 
 
 
 
 
 
2ae3596
ac38c2b
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from dotenv import find_dotenv, load_dotenv
import requests
import os
import streamlit as st

# Set up
load_dotenv(find_dotenv())
HUGGING_FACE_TOKEN = os.getenv('HUGGING_FACE_TOKEN')

API_URL = "https://router.huggingface.co/hf-inference/models/microsoft/trocr-base-handwritten"
headers = {"Authorization": f'Bearer {HUGGING_FACE_TOKEN}'}

# Query function
def query(data):
    response = requests.post(API_URL, headers=headers, data=data)

    # Print JSON response for debugging
    print(response.json())

    return response.json()[0]['generated_text']


# UI
def main():
    st.set_page_config(page_icon='random')
    st.title('READ-me : Weird Handwriting Translator')
    st.write('Upload an image of a 1 line of handwriting!')

    uploaded_file = st.file_uploader("Choose a file")

    if uploaded_file is not None:
        bytes_data = uploaded_file.getvalue()

        # Show the image
        st.image(bytes_data)

        st.header('This is what that says:')

        # Query the model
        st.write(query(bytes_data))

if __name__ == '__main__':
    main()