kgauvin603 commited on
Commit
9c0fe4b
·
verified ·
1 Parent(s): 0b8af4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -27
app.py CHANGED
@@ -32,41 +32,35 @@ 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",
46
- messages=[
47
- {"role": "system", "content": system_prompt},
48
- {"role": "user", "content": [
49
- {"type": "text", "text": user_prompt_template},
50
- {"type": "image_url", "image_url": {"url": image_url}}
51
- ]}
52
- ],
53
- max_tokens=1500
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
-
 
32
  return base64.b64encode(file.read()).decode("utf-8")
33
 
34
  # === Transcription logic ===
35
+ def transcribe_image(file):
36
+ if not file:
37
+ return "No image uploaded."
38
 
39
+ encoded = encode_image_to_base64(file)
40
+ image_url = f"data:image/jpeg;base64,{encoded}"
 
 
41
 
42
+ response = client.chat.completions.create(
43
+ model="gpt-4-turbo",
44
+ messages=[
45
+ {"role": "system", "content": system_prompt},
46
+ {"role": "user", "content": [
47
+ {"type": "text", "text": user_prompt_template},
48
+ {"type": "image_url", "image_url": {"url": image_url}}
49
+ ]}
50
+ ],
51
+ max_tokens=1500
52
+ )
53
 
54
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
55
+ return f"🗓️ Transcribed on: {timestamp}\n\n{response.choices[0].message.content}"
 
 
 
56
 
57
  # === Interface ===
58
  with gr.Blocks() as app:
59
+ gr.Markdown("## Handwritten Note Transcriber\nUpload a handwritten note image for professional transcription.")
60
+ input_file = gr.File(label="Upload image", type="file", file_types=[".jpg", ".jpeg", ".png"])
61
  output_text = gr.Textbox(label="Transcription Output", lines=30)
62
+ input_file.change(fn=transcribe_image, inputs=input_file, outputs=output_text)
63
 
64
  # === Launch ===
65
  if __name__ == "__main__":
66
  app.launch()