Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,41 +32,35 @@ def encode_image_to_base64(file):
|
|
| 32 |
return base64.b64encode(file.read()).decode("utf-8")
|
| 33 |
|
| 34 |
# === Transcription logic ===
|
| 35 |
-
def
|
| 36 |
-
if not
|
| 37 |
-
return "No
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
encoded = encode_image_to_base64(file)
|
| 42 |
-
image_url = f"data:image/jpeg;base64,{encoded}"
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 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
|
| 65 |
-
|
| 66 |
output_text = gr.Textbox(label="Transcription Output", lines=30)
|
| 67 |
-
|
| 68 |
|
| 69 |
# === Launch ===
|
| 70 |
if __name__ == "__main__":
|
| 71 |
app.launch()
|
| 72 |
-
|
|
|
|
| 32 |
return base64.b64encode(file.read()).decode("utf-8")
|
| 33 |
|
| 34 |
# === Transcription logic ===
|
| 35 |
+
def transcribe_image(file):
|
| 36 |
+
if not file:
|
| 37 |
+
return "No image uploaded."
|
| 38 |
|
| 39 |
+
encoded = encode_image_to_base64(file)
|
| 40 |
+
image_url = f"data:image/jpeg;base64,{encoded}"
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
response = client.chat.completions.create(
|
| 43 |
+
model="gpt-4-turbo",
|
| 44 |
+
messages=[
|
| 45 |
+
{"role": "system", "content": system_prompt},
|
| 46 |
+
{"role": "user", "content": [
|
| 47 |
+
{"type": "text", "text": user_prompt_template},
|
| 48 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
| 49 |
+
]}
|
| 50 |
+
],
|
| 51 |
+
max_tokens=1500
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 55 |
+
return f"🗓️ Transcribed on: {timestamp}\n\n{response.choices[0].message.content}"
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# === Interface ===
|
| 58 |
with gr.Blocks() as app:
|
| 59 |
+
gr.Markdown("## Handwritten Note Transcriber\nUpload a handwritten note image for professional transcription.")
|
| 60 |
+
input_file = gr.File(label="Upload image", type="file", file_types=[".jpg", ".jpeg", ".png"])
|
| 61 |
output_text = gr.Textbox(label="Transcription Output", lines=30)
|
| 62 |
+
input_file.change(fn=transcribe_image, inputs=input_file, outputs=output_text)
|
| 63 |
|
| 64 |
# === Launch ===
|
| 65 |
if __name__ == "__main__":
|
| 66 |
app.launch()
|
|
|