bravewiki commited on
Commit
60c15f5
·
verified ·
1 Parent(s): 131f9fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -1,31 +1,36 @@
1
  import os
2
  import streamlit as st
3
- from transformers import AutoProcessor, AutoModelForPreTraining
4
- import pytesseract
5
- import cv2
6
- import numpy as np
7
  from PIL import Image
 
 
 
8
  from huggingface_hub import login
9
 
10
  # Get the token from environment variables (set in Hugging Face Space)
11
  token = os.getenv("HF_Token")
12
  login(token)
13
 
14
- # Load the model
15
- processor = AutoProcessor.from_pretrained("google/paligemma-3b-mix-224")
16
- model = AutoModelForPreTraining.from_pretrained("google/paligemma-3b-mix-224")
17
-
18
- # Function to extract text from image using OCR
19
- def extract_text_from_image(image_file):
20
- image = Image.open(image_file)
21
- img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
22
- text = pytesseract.image_to_string(img_cv)
23
- return text
24
-
25
- # Function to get AI interpretation of the prescription
26
- def interpret_prescription(text):
27
- response = model(text)
28
- return response[0]['generated_text'].strip()
 
 
 
 
 
29
 
30
  # Set Streamlit page configuration
31
  st.set_page_config(
@@ -44,15 +49,8 @@ if uploaded_file is not None:
44
  # Display uploaded image
45
  st.image(uploaded_file, caption="Uploaded Prescription", use_column_width=True)
46
 
47
- with st.spinner("Extracting text from prescription..."):
48
- # Extract text from image using OCR
49
- extracted_text = extract_text_from_image(uploaded_file)
50
- st.subheader("Extracted Text from Prescription:")
51
- st.text(extracted_text)
52
-
53
- if extracted_text:
54
- # Interpret extracted text using the model
55
- with st.spinner("Interpreting the prescription..."):
56
- ai_response = interpret_prescription(extracted_text)
57
- st.subheader("AI Interpretation:")
58
- st.text(ai_response)
 
1
  import os
2
  import streamlit as st
3
+ import requests
 
 
 
4
  from PIL import Image
5
+ import numpy as np
6
+ from transformers import AutoTokenizer, AutoModelForCausalLM
7
+ import torch
8
  from huggingface_hub import login
9
 
10
  # Get the token from environment variables (set in Hugging Face Space)
11
  token = os.getenv("HF_Token")
12
  login(token)
13
 
14
+ # Initialize the Hugging Face model
15
+ model_name = "google/paligemma-3b-mix-224"
16
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
17
+ model = AutoModelForCausalLM.from_pretrained(model_name)
18
+
19
+ # Function to transcribe handwritten notes using Hugging Face model
20
+ def transcribe_handwriting(image):
21
+ # Convert image to array and preprocess
22
+ image = image.convert("RGB")
23
+ image = np.array(image)
24
+
25
+ # Prepare input for the model
26
+ inputs = tokenizer(image, return_tensors="pt")
27
+
28
+ # Generate output
29
+ with torch.no_grad():
30
+ outputs = model.generate(**inputs, max_length=512)
31
+
32
+ transcription = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
+ return transcription
34
 
35
  # Set Streamlit page configuration
36
  st.set_page_config(
 
49
  # Display uploaded image
50
  st.image(uploaded_file, caption="Uploaded Prescription", use_column_width=True)
51
 
52
+ with st.spinner("Transcribing handwriting..."):
53
+ # Transcribe handwritten notes
54
+ extracted_text = transcribe_handwriting(uploaded_file)
55
+ st.subheader("Transcribed Text from Prescription:")
56
+ st.text(extracted_text)