diginoron commited on
Commit
83a2a70
·
verified ·
1 Parent(s): e5574dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -9
app.py CHANGED
@@ -1,11 +1,18 @@
 
1
  import gradio as gr
2
  import pandas as pd
3
  import comtradeapicall
4
- import os
5
- from datetime import datetime
 
 
 
6
 
7
  subscription_key = os.getenv("COMTRADE_API_KEY", "")
8
- proxy_url = None
 
 
 
9
 
10
  def get_importers(hs_code: str, year: str, month: str):
11
  period = f"{year}{int(month):02d}"
@@ -28,7 +35,21 @@ def get_importers(hs_code: str, year: str, month: str):
28
  result.columns = ["کد کشور","نام کشور","ارزش CIF"]
29
  return result
30
 
31
- current_year = datetime.now().year
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  years = [str(y) for y in range(2000, current_year+1)]
33
  months = [str(m) for m in range(1, 13)]
34
 
@@ -37,14 +58,27 @@ with gr.Blocks() as demo:
37
  with gr.Row():
38
  inp_hs = gr.Textbox(label="HS Code")
39
  inp_year = gr.Dropdown(choices=years, label="سال", value=str(current_year))
40
- inp_month = gr.Dropdown(choices=months, label="ماه", value=str(datetime.now().month))
41
- btn = gr.Button("نمایش اطلاعات")
42
- out = gr.Dataframe(
43
  headers=["کد کشور","نام کشور","ارزش CIF"],
44
  datatype=["number","text","number"],
45
- interactive=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  )
47
- btn.click(get_importers, inputs=[inp_hs, inp_year, inp_month], outputs=out)
48
 
49
  if __name__ == "__main__":
50
  demo.launch()
 
1
+ import os
2
  import gradio as gr
3
  import pandas as pd
4
  import comtradeapicall
5
+ from huggingface_hub import InferenceApi
6
+
7
+ # Environment variables:
8
+ # COMTRADE_API_KEY -> برای COMTRADE (preview نیازی نیست اما برای آینده)
9
+ # HF_API_TOKEN -> برای Hugging Face Inference API
10
 
11
  subscription_key = os.getenv("COMTRADE_API_KEY", "")
12
+ hf_token = os.getenv("HF_API_TOKEN", None)
13
+ proxy_url = None
14
+
15
+ inference = InferenceApi(repo_id="google/gemma-2b", token=hf_token)
16
 
17
  def get_importers(hs_code: str, year: str, month: str):
18
  period = f"{year}{int(month):02d}"
 
35
  result.columns = ["کد کشور","نام کشور","ارزش CIF"]
36
  return result
37
 
38
+ def provide_advice(table, hs_code: str, year: str, month: str):
39
+ if not table:
40
+ return "ابتدا باید اطلاعات واردات را نمایش دهید."
41
+ df = pd.DataFrame(table, columns=["کد کشور","نام کشور","ارزش CIF"])
42
+ table_str = df.to_markdown(index=False)
43
+ period = f"{year}/{int(month):02d}"
44
+ prompt = (
45
+ f"جدول زیر اطلاعات کشورهایی است که کالا با کد HS {hs_code} را در دوره {period} وارد کرده‌اند:\n"
46
+ f"{table_str}\n\n"
47
+ "لطفاً بر اساس این اطلاعات دو پاراگراف مشاوره تخصصی بنویسید."
48
+ )
49
+ response = inference(prompt)
50
+ return response
51
+
52
+ current_year = pd.Timestamp.now().year
53
  years = [str(y) for y in range(2000, current_year+1)]
54
  months = [str(m) for m in range(1, 13)]
55
 
 
58
  with gr.Row():
59
  inp_hs = gr.Textbox(label="HS Code")
60
  inp_year = gr.Dropdown(choices=years, label="سال", value=str(current_year))
61
+ inp_month = gr.Dropdown(choices=months, label="ماه", value=str(pd.Timestamp.now().month))
62
+ btn_show = gr.Button("نمایش اطلاعات")
63
+ out_table = gr.Dataframe(
64
  headers=["کد کشور","نام کشور","ارزش CIF"],
65
  datatype=["number","text","number"],
66
+ interactive=False,
67
+ )
68
+ btn_show.click(
69
+ fn=get_importers,
70
+ inputs=[inp_hs, inp_year, inp_month],
71
+ outputs=out_table
72
+ )
73
+
74
+ btn_advice = gr.Button("ارائه مشاوره تخصصی")
75
+ out_advice = gr.Textbox(label="مشاوره تخصصی")
76
+
77
+ btn_advice.click(
78
+ fn=provide_advice,
79
+ inputs=[out_table, inp_hs, inp_year, inp_month],
80
+ outputs=out_advice
81
  )
 
82
 
83
  if __name__ == "__main__":
84
  demo.launch()