Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import base64
|
|
|
|
| 3 |
import threading
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
|
@@ -14,6 +15,7 @@ except ImportError:
|
|
| 14 |
|
| 15 |
ORIGINAL_MODEL_ID = "openbmb/MiniCPM-V-4.6"
|
| 16 |
FINETUNED_MODEL_ID = "jon-fernandes/noteworthy"
|
|
|
|
| 17 |
NOTES_PROMPT = "Transcribe the musical notes in this image."
|
| 18 |
|
| 19 |
print("Loading processor...")
|
|
@@ -93,31 +95,21 @@ def stream_model(model, image: Image.Image):
|
|
| 93 |
thread.join()
|
| 94 |
|
| 95 |
|
| 96 |
-
def
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
image = Image.open(image_path).convert("RGB")
|
| 102 |
|
| 103 |
-
finetuned_text = ""
|
| 104 |
-
for chunk in stream_model(finetuned_model, image):
|
| 105 |
-
finetuned_text += chunk
|
| 106 |
-
yield finetuned_text, ""
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
original_text += chunk
|
| 111 |
-
yield finetuned_text, original_text
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
yield "Please upload an image."
|
| 117 |
return
|
| 118 |
|
| 119 |
-
yield "Calling GPT-5.5..."
|
| 120 |
-
|
| 121 |
try:
|
| 122 |
from openai import OpenAI
|
| 123 |
|
|
@@ -128,9 +120,9 @@ def predict_gpt(image_path):
|
|
| 128 |
ext = "jpeg"
|
| 129 |
mime = f"image/{ext}" if ext in ("png", "jpeg", "gif", "webp") else "image/jpeg"
|
| 130 |
|
| 131 |
-
client = OpenAI(api_key=
|
| 132 |
response = client.chat.completions.create(
|
| 133 |
-
model=
|
| 134 |
messages=[{
|
| 135 |
"role": "user",
|
| 136 |
"content": [
|
|
@@ -157,8 +149,60 @@ def predict_gpt(image_path):
|
|
| 157 |
yield f"[Error: {e}]"
|
| 158 |
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
if HAS_SPACES:
|
| 161 |
-
|
| 162 |
|
| 163 |
|
| 164 |
with gr.Blocks(title="Noteworthy — Sheet Music Transcription") as demo:
|
|
@@ -191,19 +235,14 @@ with gr.Blocks(title="Noteworthy — Sheet Music Transcription") as demo:
|
|
| 191 |
lines=20,
|
| 192 |
)
|
| 193 |
gpt_output = gr.Textbox(
|
| 194 |
-
label=
|
| 195 |
lines=20,
|
| 196 |
)
|
| 197 |
|
| 198 |
notes_btn.click(
|
| 199 |
-
fn=
|
| 200 |
-
inputs=[image_input],
|
| 201 |
-
outputs=[finetuned_output, original_output],
|
| 202 |
-
)
|
| 203 |
-
notes_btn.click(
|
| 204 |
-
fn=predict_gpt,
|
| 205 |
inputs=[image_input],
|
| 206 |
-
outputs=[gpt_output],
|
| 207 |
)
|
| 208 |
|
| 209 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 1 |
import os
|
| 2 |
import base64
|
| 3 |
+
from queue import Queue
|
| 4 |
import threading
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
|
|
|
| 15 |
|
| 16 |
ORIGINAL_MODEL_ID = "openbmb/MiniCPM-V-4.6"
|
| 17 |
FINETUNED_MODEL_ID = "jon-fernandes/noteworthy"
|
| 18 |
+
GPT_MODEL_ID = "gpt-5.5"
|
| 19 |
NOTES_PROMPT = "Transcribe the musical notes in this image."
|
| 20 |
|
| 21 |
print("Loading processor...")
|
|
|
|
| 95 |
thread.join()
|
| 96 |
|
| 97 |
|
| 98 |
+
def stream_model_text(model, image: Image.Image):
|
| 99 |
+
text = ""
|
| 100 |
+
for chunk in stream_model(model, image):
|
| 101 |
+
text += chunk
|
| 102 |
+
yield text
|
|
|
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
def stream_gpt_text(image_path):
|
| 106 |
+
yield f"Calling {GPT_MODEL_ID}..."
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
| 109 |
+
if not api_key:
|
| 110 |
+
yield "[Error: OPENAI_API_KEY is not set.]"
|
|
|
|
| 111 |
return
|
| 112 |
|
|
|
|
|
|
|
| 113 |
try:
|
| 114 |
from openai import OpenAI
|
| 115 |
|
|
|
|
| 120 |
ext = "jpeg"
|
| 121 |
mime = f"image/{ext}" if ext in ("png", "jpeg", "gif", "webp") else "image/jpeg"
|
| 122 |
|
| 123 |
+
client = OpenAI(api_key=api_key)
|
| 124 |
response = client.chat.completions.create(
|
| 125 |
+
model=GPT_MODEL_ID,
|
| 126 |
messages=[{
|
| 127 |
"role": "user",
|
| 128 |
"content": [
|
|
|
|
| 149 |
yield f"[Error: {e}]"
|
| 150 |
|
| 151 |
|
| 152 |
+
def _run_stream(index, stream, updates):
|
| 153 |
+
try:
|
| 154 |
+
for text in stream:
|
| 155 |
+
updates.put((index, text))
|
| 156 |
+
except Exception as e:
|
| 157 |
+
updates.put((index, f"[Error: {e}]"))
|
| 158 |
+
finally:
|
| 159 |
+
updates.put((index, None))
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def predict_all(image_path):
|
| 163 |
+
if image_path is None:
|
| 164 |
+
message = "Please upload an image."
|
| 165 |
+
yield message, message, message
|
| 166 |
+
return
|
| 167 |
+
|
| 168 |
+
image = Image.open(image_path).convert("RGB")
|
| 169 |
+
updates = Queue()
|
| 170 |
+
outputs = ["", "", ""]
|
| 171 |
+
|
| 172 |
+
streams = [
|
| 173 |
+
stream_model_text(finetuned_model, image.copy()),
|
| 174 |
+
stream_model_text(original_model, image.copy()),
|
| 175 |
+
stream_gpt_text(image_path),
|
| 176 |
+
]
|
| 177 |
+
|
| 178 |
+
threads = [
|
| 179 |
+
threading.Thread(
|
| 180 |
+
target=_run_stream,
|
| 181 |
+
args=(index, stream, updates),
|
| 182 |
+
daemon=True,
|
| 183 |
+
)
|
| 184 |
+
for index, stream in enumerate(streams)
|
| 185 |
+
]
|
| 186 |
+
|
| 187 |
+
for thread in threads:
|
| 188 |
+
thread.start()
|
| 189 |
+
|
| 190 |
+
running = len(threads)
|
| 191 |
+
while running:
|
| 192 |
+
index, text = updates.get()
|
| 193 |
+
if text is None:
|
| 194 |
+
running -= 1
|
| 195 |
+
continue
|
| 196 |
+
|
| 197 |
+
outputs[index] = text
|
| 198 |
+
yield tuple(outputs)
|
| 199 |
+
|
| 200 |
+
for thread in threads:
|
| 201 |
+
thread.join()
|
| 202 |
+
|
| 203 |
+
|
| 204 |
if HAS_SPACES:
|
| 205 |
+
predict_all = spaces.GPU(duration=180)(predict_all)
|
| 206 |
|
| 207 |
|
| 208 |
with gr.Blocks(title="Noteworthy — Sheet Music Transcription") as demo:
|
|
|
|
| 235 |
lines=20,
|
| 236 |
)
|
| 237 |
gpt_output = gr.Textbox(
|
| 238 |
+
label=GPT_MODEL_ID.upper(),
|
| 239 |
lines=20,
|
| 240 |
)
|
| 241 |
|
| 242 |
notes_btn.click(
|
| 243 |
+
fn=predict_all,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
inputs=[image_input],
|
| 245 |
+
outputs=[finetuned_output, original_output, gpt_output],
|
| 246 |
)
|
| 247 |
|
| 248 |
demo.launch(theme=gr.themes.Soft())
|