jon-fernandes commited on
Commit
b893ff7
·
verified ·
1 Parent(s): c3dd580

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +29 -13
app.py CHANGED
@@ -93,9 +93,9 @@ def stream_model(model, image: Image.Image):
93
  thread.join()
94
 
95
 
96
- def predict(image_path):
97
  if image_path is None:
98
- yield "Please upload an image.", "Please upload an image.", "Please upload an image."
99
  return
100
 
101
  image = Image.open(image_path).convert("RGB")
@@ -103,16 +103,24 @@ def predict(image_path):
103
  finetuned_text = ""
104
  for chunk in stream_model(finetuned_model, image):
105
  finetuned_text += chunk
106
- yield finetuned_text, "", ""
107
 
108
  original_text = ""
109
  for chunk in stream_model(original_model, image):
110
  original_text += chunk
111
- yield finetuned_text, original_text, ""
 
 
 
 
 
 
 
 
112
 
113
- gpt_text = ""
114
  try:
115
  from openai import OpenAI
 
116
  with open(image_path, "rb") as f:
117
  image_data = base64.b64encode(f.read()).decode("utf-8")
118
  ext = os.path.splitext(image_path)[1].lstrip(".").lower()
@@ -133,20 +141,23 @@ def predict(image_path):
133
  max_completion_tokens=1024,
134
  stream=True,
135
  )
 
 
136
  for chunk in stream:
137
  delta = chunk.choices[0].delta.content
138
  if delta:
139
  gpt_text += delta
140
- yield finetuned_text, original_text, gpt_text
141
- except Exception as e:
142
- gpt_text = f"[Error: {e}]"
143
- yield finetuned_text, original_text, gpt_text
144
 
145
- yield finetuned_text, original_text, gpt_text
 
146
 
147
 
148
  if HAS_SPACES:
149
- predict = spaces.GPU(duration=180)(predict)
150
 
151
 
152
  with gr.Blocks(title="Noteworthy — Sheet Music Transcription") as demo:
@@ -183,9 +194,14 @@ with gr.Blocks(title="Noteworthy — Sheet Music Transcription") as demo:
183
  )
184
 
185
  notes_btn.click(
186
- fn=predict,
 
 
 
 
 
187
  inputs=[image_input],
188
- outputs=[finetuned_output, original_output, gpt_output],
189
  )
190
 
191
  demo.launch(theme=gr.themes.Soft())
 
93
  thread.join()
94
 
95
 
96
+ def predict_local(image_path):
97
  if image_path is None:
98
+ yield "Please upload an image.", "Please upload an image."
99
  return
100
 
101
  image = Image.open(image_path).convert("RGB")
 
103
  finetuned_text = ""
104
  for chunk in stream_model(finetuned_model, image):
105
  finetuned_text += chunk
106
+ yield finetuned_text, ""
107
 
108
  original_text = ""
109
  for chunk in stream_model(original_model, image):
110
  original_text += chunk
111
+ yield finetuned_text, original_text
112
+
113
+
114
+ def predict_gpt(image_path):
115
+ if image_path is None:
116
+ yield "Please upload an image."
117
+ return
118
+
119
+ yield "Calling GPT-5.5..."
120
 
 
121
  try:
122
  from openai import OpenAI
123
+
124
  with open(image_path, "rb") as f:
125
  image_data = base64.b64encode(f.read()).decode("utf-8")
126
  ext = os.path.splitext(image_path)[1].lstrip(".").lower()
 
141
  max_completion_tokens=1024,
142
  stream=True,
143
  )
144
+
145
+ gpt_text = ""
146
  for chunk in stream:
147
  delta = chunk.choices[0].delta.content
148
  if delta:
149
  gpt_text += delta
150
+ yield gpt_text
151
+
152
+ if not gpt_text:
153
+ yield "[No response received from GPT-5.5]"
154
 
155
+ except Exception as e:
156
+ yield f"[Error: {e}]"
157
 
158
 
159
  if HAS_SPACES:
160
+ predict_local = spaces.GPU(duration=180)(predict_local)
161
 
162
 
163
  with gr.Blocks(title="Noteworthy — Sheet Music Transcription") as demo:
 
194
  )
195
 
196
  notes_btn.click(
197
+ fn=predict_local,
198
+ inputs=[image_input],
199
+ outputs=[finetuned_output, original_output],
200
+ )
201
+ notes_btn.click(
202
+ fn=predict_gpt,
203
  inputs=[image_input],
204
+ outputs=[gpt_output],
205
  )
206
 
207
  demo.launch(theme=gr.themes.Soft())