Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from datetime import datetime
|
|
| 4 |
from openai import OpenAI
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
# ===
|
| 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.")
|
|
@@ -27,20 +27,19 @@ user_prompt_template = (
|
|
| 27 |
"author, but enhance readability and flow. Do not add embellishments or AI-style phrasing."
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# ===
|
| 31 |
-
def encode_image_to_base64(
|
| 32 |
-
|
| 33 |
-
return base64.b64encode(image_bytes).decode("utf-8")
|
| 34 |
|
| 35 |
-
# === Transcription
|
| 36 |
def transcribe_images(files):
|
| 37 |
if not files:
|
| 38 |
return "No images uploaded."
|
| 39 |
|
| 40 |
results = []
|
| 41 |
for file in files:
|
| 42 |
-
|
| 43 |
-
image_url = f"data:image/jpeg;base64,{
|
| 44 |
|
| 45 |
response = client.chat.completions.create(
|
| 46 |
model="gpt-4-turbo",
|
|
@@ -55,25 +54,19 @@ def transcribe_images(files):
|
|
| 55 |
)
|
| 56 |
|
| 57 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 58 |
-
|
| 59 |
-
results.append(
|
| 60 |
|
| 61 |
return "\n\n---\n\n".join(results)
|
| 62 |
|
| 63 |
-
# ===
|
| 64 |
with gr.Blocks() as app:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
multifile=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 |
-
|
|
|
|
| 4 |
from openai import OpenAI
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# === OpenAI API Setup ===
|
| 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.")
|
|
|
|
| 27 |
"author, but enhance readability and flow. Do not add embellishments or AI-style phrasing."
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# === Encode uploaded file ===
|
| 31 |
+
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",
|
|
|
|
| 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 |
|
|
|