frendyrachman commited on
Commit
f95f02e
Β·
verified Β·
1 Parent(s): b1520c8

Update app.py

Browse files

Add total token counter

Files changed (1) hide show
  1. app.py +7 -4
app.py CHANGED
@@ -145,6 +145,8 @@ def gemini_analysis(images, tanggal_berangkat, tanggal_pulang, api_key):
145
  response_mime_type="application/json"
146
  )
147
  )
 
 
148
  raw_output = response.text
149
  # βœ… Inisialisasi variabel default
150
  analysis = {}
@@ -170,7 +172,7 @@ Halo Kak selamat malam 😊
170
  form_filling_str = json.dumps(form_filling, indent=2, ensure_ascii=False)
171
  except Exception as e:
172
  print(f"Error parsing JSON: {e}")
173
- return raw_output, analysis_str, summary, invalid_list_str, notice_msg, form_filling_str
174
 
175
  def process_and_zip_all_images(images, api_key, zip_name="All_PDF_Docs.zip"):
176
  # Inisialisasi Gemini client
@@ -251,7 +253,7 @@ def main_process(files, tanggal_berangkat, tanggal_pulang, api_key, dpi):
251
  raise ValueError(f"File {file_path} is not a valid image, PDF, or ZIP.")
252
 
253
  # Generate summary from images
254
- raw_output, analysis_str, summary, invalid_list_str, notice_msg, form_filling_str = gemini_analysis(all_images, tanggal_berangkat, tanggal_pulang, api_key)
255
  rdf = random.randint(5, 10)
256
  time.sleep(rdf)
257
 
@@ -275,7 +277,7 @@ def main_process(files, tanggal_berangkat, tanggal_pulang, api_key, dpi):
275
 
276
  # Filtering the file
277
  zip_file_path = process_and_zip_all_images(all_images, api_key, zip_name=f'All_PDF_Docs_{base_name}.zip')
278
- return temp_docx_path, form_filling_str, zip_file_path, invalid_list_str, raw_output, summary, notice_msg
279
 
280
 
281
  # Gradio UI update: add ".zip" to accepted file types
@@ -330,11 +332,12 @@ with gr.Blocks() as demo:
330
  notice_msg = gr.Textbox(label="πŸ“ NOTIFICATION MSG", lines=10)
331
  gr.Markdown("## πŸ“ RAW OUTPUT FROM AI")
332
  raw_output = gr.Textbox(label="πŸ“ RAW OUTPUT", lines=20)
 
333
 
334
  run_btn.click(
335
  fn=main_process,
336
  inputs=[file_input, tanggal_berangkat, tanggal_pulang, api_key, dpi_slider],
337
- outputs=[download_output_docx, form_filling_output, download_valid_zip, invalid_list_output, raw_output, summary_output, notice_msg]
338
  )
339
 
340
  demo.launch(debug=True)
 
145
  response_mime_type="application/json"
146
  )
147
  )
148
+ total_tokens = client.models.count_tokens(
149
+ model="gemini-2.0-flash-lite", contents=[prompt_with_date] + images)
150
  raw_output = response.text
151
  # βœ… Inisialisasi variabel default
152
  analysis = {}
 
172
  form_filling_str = json.dumps(form_filling, indent=2, ensure_ascii=False)
173
  except Exception as e:
174
  print(f"Error parsing JSON: {e}")
175
+ return raw_output, analysis_str, summary, invalid_list_str, notice_msg, form_filling_str, total_tokens
176
 
177
  def process_and_zip_all_images(images, api_key, zip_name="All_PDF_Docs.zip"):
178
  # Inisialisasi Gemini client
 
253
  raise ValueError(f"File {file_path} is not a valid image, PDF, or ZIP.")
254
 
255
  # Generate summary from images
256
+ raw_output, analysis_str, summary, invalid_list_str, notice_msg, form_filling_str, total_tokens = gemini_analysis(all_images, tanggal_berangkat, tanggal_pulang, api_key)
257
  rdf = random.randint(5, 10)
258
  time.sleep(rdf)
259
 
 
277
 
278
  # Filtering the file
279
  zip_file_path = process_and_zip_all_images(all_images, api_key, zip_name=f'All_PDF_Docs_{base_name}.zip')
280
+ return temp_docx_path, form_filling_str, zip_file_path, invalid_list_str, raw_output, summary, notice_msg, total_tokens
281
 
282
 
283
  # Gradio UI update: add ".zip" to accepted file types
 
332
  notice_msg = gr.Textbox(label="πŸ“ NOTIFICATION MSG", lines=10)
333
  gr.Markdown("## πŸ“ RAW OUTPUT FROM AI")
334
  raw_output = gr.Textbox(label="πŸ“ RAW OUTPUT", lines=20)
335
+ token_counter = gr.Textbox(label="Token Usage Count", lines=5)
336
 
337
  run_btn.click(
338
  fn=main_process,
339
  inputs=[file_input, tanggal_berangkat, tanggal_pulang, api_key, dpi_slider],
340
+ outputs=[download_output_docx, form_filling_output, download_valid_zip, invalid_list_output, raw_output, summary_output, notice_msg, token_counter]
341
  )
342
 
343
  demo.launch(debug=True)