kgauvin603 commited on
Commit
52f96c0
·
verified ·
1 Parent(s): 76a0a55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -24
app.py CHANGED
@@ -1,10 +1,15 @@
1
  import base64
 
2
  from datetime import datetime
3
  from openai import OpenAI
4
  import gradio as gr
5
 
6
- # === Initialize OpenAI Client ===
7
- client = OpenAI()
 
 
 
 
8
 
9
  # === Prompts ===
10
  system_prompt = (
@@ -28,37 +33,48 @@ def encode_image_to_base64(image_file):
28
  return base64.b64encode(image_bytes).decode("utf-8")
29
 
30
  # === Transcription function ===
31
- def transcribe_image(image):
32
- if image is None:
33
- return "No image uploaded."
 
 
 
 
 
34
 
35
- encoded_image = encode_image_to_base64(image)
36
- image_url = f"data:image/jpeg;base64,{encoded_image}"
 
 
 
 
 
 
 
 
 
37
 
38
- response = client.chat.completions.create(
39
- model="gpt-4-turbo",
40
- messages=[
41
- {"role": "system", "content": system_prompt},
42
- {"role": "user", "content": [
43
- {"type": "text", "text": user_prompt_template},
44
- {"type": "image_url", "image_url": {"url": image_url}}
45
- ]}
46
- ],
47
- max_tokens=1500
48
- )
49
 
50
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
51
- return f"🗓️ Transcribed on: {timestamp}\n\n{response.choices[0].message.content}"
52
 
53
  # === Gradio Interface ===
54
  app = gr.Interface(
55
- fn=transcribe_image,
56
- inputs=gr.Image(type="file", label="Upload handwritten note (image)"),
57
- outputs=gr.Textbox(label="Transcribed Output"),
 
 
 
 
 
58
  title="Handwritten Note Transcriber",
59
- description="Upload an image of handwritten notes to receive a clean, professional transcription."
60
  )
61
 
62
  # === Launch ===
63
  if __name__ == "__main__":
64
  app.launch()
 
 
1
  import base64
2
+ import os
3
  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.")
11
+
12
+ client = OpenAI(api_key=openai_api_key)
13
 
14
  # === Prompts ===
15
  system_prompt = (
 
33
  return base64.b64encode(image_bytes).decode("utf-8")
34
 
35
  # === Transcription function ===
36
+ def transcribe_images(images):
37
+ if not images:
38
+ return "No images uploaded."
39
+
40
+ results = []
41
+ for image in images:
42
+ encoded_image = encode_image_to_base64(image)
43
+ image_url = f"data:image/jpeg;base64,{encoded_image}"
44
 
45
+ response = client.chat.completions.create(
46
+ model="gpt-4-turbo",
47
+ messages=[
48
+ {"role": "system", "content": system_prompt},
49
+ {"role": "user", "content": [
50
+ {"type": "text", "text": user_prompt_template},
51
+ {"type": "image_url", "image_url": {"url": image_url}}
52
+ ]}
53
+ ],
54
+ max_tokens=1500
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 ===
64
  app = gr.Interface(
65
+ fn=transcribe_images,
66
+ inputs=gr.File(
67
+ type="file",
68
+ label="Upload handwritten note images",
69
+ file_types=[".jpg", ".jpeg", ".png"],
70
+ multiple=True
71
+ ),
72
+ outputs=gr.Textbox(label="Transcribed Output", lines=30),
73
  title="Handwritten Note Transcriber",
74
+ description="Upload one or more images of handwritten notes to receive clean, professional transcriptions."
75
  )
76
 
77
  # === Launch ===
78
  if __name__ == "__main__":
79
  app.launch()
80
+