Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdd token cost in IDR
app.py
CHANGED
|
@@ -147,6 +147,7 @@ def gemini_analysis(images, tanggal_berangkat, tanggal_pulang, api_key):
|
|
| 147 |
token_counter = client.models.count_tokens(
|
| 148 |
model="gemini-2.0-flash-lite", contents=[prompt_with_date] + images)
|
| 149 |
total_tokens = token_counter.total_tokens
|
|
|
|
| 150 |
raw_output = response.text
|
| 151 |
# β
Inisialisasi variabel default
|
| 152 |
analysis = {}
|
|
@@ -171,7 +172,7 @@ Halo Kak selamat malam π
|
|
| 171 |
form_filling_str = json.dumps(form_filling, indent=2, ensure_ascii=False)
|
| 172 |
except Exception as e:
|
| 173 |
print(f"Error parsing JSON: {e}")
|
| 174 |
-
return raw_output, analysis_str, summary, invalid_list_str, notice_msg, form_filling_str,
|
| 175 |
|
| 176 |
def process_and_zip_all_images(images, api_key, zip_name="All_PDF_Docs.zip"):
|
| 177 |
# Inisialisasi Gemini client
|
|
@@ -252,7 +253,7 @@ def main_process(files, tanggal_berangkat, tanggal_pulang, api_key, dpi):
|
|
| 252 |
raise ValueError(f"File {file_path} is not a valid image, PDF, or ZIP.")
|
| 253 |
|
| 254 |
# Generate summary from images
|
| 255 |
-
raw_output, analysis_str, summary, invalid_list_str, notice_msg, form_filling_str,
|
| 256 |
rdf = random.randint(5, 10)
|
| 257 |
time.sleep(rdf)
|
| 258 |
|
|
@@ -276,7 +277,7 @@ def main_process(files, tanggal_berangkat, tanggal_pulang, api_key, dpi):
|
|
| 276 |
|
| 277 |
# Filtering the file
|
| 278 |
zip_file_path = process_and_zip_all_images(all_images, api_key, zip_name=f'All_PDF_Docs_{base_name}.zip')
|
| 279 |
-
return temp_docx_path, form_filling_str, zip_file_path, invalid_list_str, raw_output, summary, notice_msg,
|
| 280 |
|
| 281 |
|
| 282 |
# Gradio UI update: add ".zip" to accepted file types
|
|
@@ -331,12 +332,13 @@ with gr.Blocks() as demo:
|
|
| 331 |
notice_msg = gr.Textbox(label="π NOTIFICATION MSG", lines=10)
|
| 332 |
gr.Markdown("## π RAW OUTPUT FROM AI")
|
| 333 |
raw_output = gr.Textbox(label="π RAW OUTPUT", lines=20)
|
| 334 |
-
|
|
|
|
| 335 |
|
| 336 |
run_btn.click(
|
| 337 |
fn=main_process,
|
| 338 |
inputs=[file_input, tanggal_berangkat, tanggal_pulang, api_key, dpi_slider],
|
| 339 |
-
outputs=[download_output_docx, form_filling_output, download_valid_zip, invalid_list_output, raw_output, summary_output, notice_msg,
|
| 340 |
)
|
| 341 |
|
| 342 |
demo.launch(debug=True)
|
|
|
|
| 147 |
token_counter = client.models.count_tokens(
|
| 148 |
model="gemini-2.0-flash-lite", contents=[prompt_with_date] + images)
|
| 149 |
total_tokens = token_counter.total_tokens
|
| 150 |
+
total_tokens_in_idr = total_tokens*5/1000
|
| 151 |
raw_output = response.text
|
| 152 |
# β
Inisialisasi variabel default
|
| 153 |
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_in_idr
|
| 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_in_idr = 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_in_idr
|
| 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 |
+
gr.Markdown("Token cost in IDR")
|
| 336 |
+
total_tokens_in_idr = gr.Textbox(label="Token cost in IDR", lines=5)
|
| 337 |
|
| 338 |
run_btn.click(
|
| 339 |
fn=main_process,
|
| 340 |
inputs=[file_input, tanggal_berangkat, tanggal_pulang, api_key, dpi_slider],
|
| 341 |
+
outputs=[download_output_docx, form_filling_output, download_valid_zip, invalid_list_output, raw_output, summary_output, notice_msg, total_tokens_in_idr]
|
| 342 |
)
|
| 343 |
|
| 344 |
demo.launch(debug=True)
|