kgauvin603 commited on
Commit
0b8af4e
·
verified ·
1 Parent(s): 702bcc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -21
app.py CHANGED
@@ -4,7 +4,7 @@ from datetime import datetime
4
  from openai import OpenAI
5
  import gradio as gr
6
 
7
- # === Initialize OpenAI Client using Environment Variable ===
8
  openai_api_key = os.environ.get("OPENAI_API_KEY")
9
  if not openai_api_key:
10
  raise ValueError("OPENAI_API_KEY environment variable is not set.")
@@ -27,20 +27,19 @@ user_prompt_template = (
27
  "author, but enhance readability and flow. Do not add embellishments or AI-style phrasing."
28
  )
29
 
30
- # === Image processing ===
31
- def encode_image_to_base64(image_file):
32
- image_bytes = image_file.read()
33
- return base64.b64encode(image_bytes).decode("utf-8")
34
 
35
- # === Transcription function ===
36
  def transcribe_images(files):
37
  if not files:
38
  return "No images uploaded."
39
 
40
  results = []
41
  for file in files:
42
- encoded_image = encode_image_to_base64(file)
43
- image_url = f"data:image/jpeg;base64,{encoded_image}"
44
 
45
  response = client.chat.completions.create(
46
  model="gpt-4-turbo",
@@ -55,25 +54,19 @@ def transcribe_images(files):
55
  )
56
 
57
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
58
- result_text = f"🗓️ Transcribed on: {timestamp}\n\n{response.choices[0].message.content}"
59
- results.append(result_text)
60
 
61
  return "\n\n---\n\n".join(results)
62
 
63
- # === Gradio Interface using UploadButton ===
64
  with gr.Blocks() as app:
65
- with gr.Row():
66
- uploader = gr.UploadButton(
67
- label="Upload handwritten note images",
68
- file_types=[".jpg", ".jpeg", ".png"],
69
- multifile=True
70
- )
71
- output_box = gr.Textbox(label="Transcribed Output", lines=30)
72
-
73
- uploader.change(fn=transcribe_images, inputs=uploader, outputs=output_box)
74
 
75
  # === Launch ===
76
  if __name__ == "__main__":
77
  app.launch()
78
 
79
-
 
4
  from openai import OpenAI
5
  import gradio as gr
6
 
7
+ # === OpenAI API Setup ===
8
  openai_api_key = os.environ.get("OPENAI_API_KEY")
9
  if not openai_api_key:
10
  raise ValueError("OPENAI_API_KEY environment variable is not set.")
 
27
  "author, but enhance readability and flow. Do not add embellishments or AI-style phrasing."
28
  )
29
 
30
+ # === Encode uploaded file ===
31
+ def encode_image_to_base64(file):
32
+ return base64.b64encode(file.read()).decode("utf-8")
 
33
 
34
+ # === Transcription logic ===
35
  def transcribe_images(files):
36
  if not files:
37
  return "No images uploaded."
38
 
39
  results = []
40
  for file in files:
41
+ encoded = encode_image_to_base64(file)
42
+ image_url = f"data:image/jpeg;base64,{encoded}"
43
 
44
  response = client.chat.completions.create(
45
  model="gpt-4-turbo",
 
54
  )
55
 
56
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
57
+ result = f"🗓️ Transcribed on: {timestamp}\n\n{response.choices[0].message.content}"
58
+ results.append(result)
59
 
60
  return "\n\n---\n\n".join(results)
61
 
62
+ # === Interface ===
63
  with gr.Blocks() as app:
64
+ gr.Markdown("## Handwritten Note Transcriber\nUpload one or more handwritten note images for professional transcription.")
65
+ input_files = gr.File(label="Upload images", type="file", file_types=[".jpg", ".jpeg", ".png"], multiple=True)
66
+ output_text = gr.Textbox(label="Transcription Output", lines=30)
67
+ input_files.change(fn=transcribe_images, inputs=input_files, outputs=output_text)
 
 
 
 
 
68
 
69
  # === Launch ===
70
  if __name__ == "__main__":
71
  app.launch()
72