Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 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 = (
|
| 16 |
+
"You are a detail-oriented assistant that specializes in transcribing and polishing "
|
| 17 |
+
"handwritten notes from images. Your goal is to turn rough, casual, or handwritten "
|
| 18 |
+
"content into clean, structured, and professional-looking text that sounds like it "
|
| 19 |
+
"was written by a human—not an AI. You do not include icons, emojis, or suggest next "
|
| 20 |
+
"steps unless explicitly instructed."
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
user_prompt_template = (
|
| 24 |
+
"You will receive an image of handwritten notes. Transcribe the content accurately, "
|
| 25 |
+
"correcting any spelling or grammar issues. Then, organize it clearly with headings, "
|
| 26 |
+
"bullet points, and proper formatting. Maintain the original intent and voice of the "
|
| 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",
|
| 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 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 |
+
file_types_multiple=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 |
|