kgauvin603 commited on
Commit
a072d91
·
verified ·
1 Parent(s): d5a12be

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import os
3
+ from datetime import datetime
4
+ from openai import OpenAI
5
+ import gradio as gr
6
+ import oci
7
+
8
+ # === OpenAI API Setup ===
9
+ openai_api_key = os.environ.get("OPENAI_API_KEY")
10
+ if not openai_api_key:
11
+ raise ValueError("OPENAI_API_KEY environment variable is not set.")
12
+
13
+ client = OpenAI(api_key=openai_api_key)
14
+
15
+ # === OCI Object Storage Setup ===
16
+ oci_config = {
17
+ "user": os.environ.get("OCI_USER"),
18
+ "tenancy": os.environ.get("OCI_TENANCY"),
19
+ "fingerprint": os.environ.get("OCI_FINGERPRINT"),
20
+ "region": os.environ.get("OCI_REGION"),
21
+ "key_content": os.environ.get("OCI_PRIVATE_KEY")
22
+ }
23
+
24
+ # Optional: access OCI Object Storage (if needed)
25
+ try:
26
+ object_storage = oci.object_storage.ObjectStorageClient(oci_config)
27
+ except Exception as e:
28
+ print("Failed to initialize OCI Object Storage client:", e)
29
+
30
+ # === Prompts ===
31
+ system_prompt = (
32
+ "You are a detail-oriented assistant that specializes in transcribing and polishing "
33
+ "handwritten notes from images. Your goal is to turn rough, casual, or handwritten "
34
+ "content into clean, structured, and professional-looking text that sounds like it "
35
+ "was written by a human—not an AI. You do not include icons, emojis, or suggest next "
36
+ "steps unless explicitly instructed."
37
+ )
38
+
39
+ user_prompt_template = (
40
+ "You will receive an image of handwritten notes. Transcribe the content accurately, "
41
+ "correcting any spelling or grammar issues. Then, organize it clearly with headings, "
42
+ "bullet points, and proper formatting. Maintain the original intent and voice of the "
43
+ "author, but enhance readability and flow. Do not add embellishments or AI-style phrasing."
44
+ )
45
+
46
+ # === Encode uploaded bytes ===
47
+ def encode_image_to_base64(file_bytes):
48
+ return base64.b64encode(file_bytes).decode("utf-8")
49
+
50
+ # === Transcription logic ===
51
+ def transcribe_image(file_bytes):
52
+ if not file_bytes:
53
+ return "No image uploaded."
54
+
55
+ encoded = encode_image_to_base64(file_bytes)
56
+ image_url = f"data:image/jpeg;base64,{encoded}"
57
+
58
+ response = client.chat.completions.create(
59
+ model="gpt-4-turbo",
60
+ messages=[
61
+ {"role": "system", "content": system_prompt},
62
+ {"role": "user", "content": [
63
+ {"type": "text", "text": user_prompt_template},
64
+ {"type": "image_url", "image_url": {"url": image_url}}
65
+ ]}
66
+ ],
67
+ max_tokens=1500
68
+ )
69
+
70
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
71
+ return f"🗓️ Transcribed on: {timestamp}\n\n{response.choices[0].message.content}"
72
+
73
+ # === Gradio Interface ===
74
+ with gr.Blocks() as app:
75
+ gr.Markdown("## Handwritten Note Transcriber\nUpload a handwritten note image for professional transcription.")
76
+ input_file = gr.File(label="Upload image", type="binary", file_types=[".jpg", ".jpeg", ".png"])
77
+ output_text = gr.Textbox(label="Transcription Output", lines=30)
78
+ input_file.change(fn=transcribe_image, inputs=input_file, outputs=output_text)
79
+
80
+ # === Launch App ===
81
+ if __name__ == "__main__":
82
+ app.launch(share=True)