kgauvin603 commited on
Commit
d675a30
·
verified ·
1 Parent(s): 23a92be

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = (
11
+ "You are a detail-oriented assistant that specializes in transcribing and polishing "
12
+ "handwritten notes from images. Your goal is to turn rough, casual, or handwritten "
13
+ "content into clean, structured, and professional-looking text that sounds like it "
14
+ "was written by a human—not an AI. You do not include icons, emojis, or suggest next "
15
+ "steps unless explicitly instructed."
16
+ )
17
+
18
+ user_prompt_template = (
19
+ "You will receive an image of handwritten notes. Transcribe the content accurately, "
20
+ "correcting any spelling or grammar issues. Then, organize it clearly with headings, "
21
+ "bullet points, and proper formatting. Maintain the original intent and voice of the "
22
+ "author, but enhance readability and flow. Do not add embellishments or AI-style phrasing."
23
+ )
24
+
25
+ # === Image processing ===
26
+ def encode_image_to_base64(image_file):
27
+ image_bytes = image_file.read()
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()