diginoron commited on
Commit
a556cf5
·
verified ·
1 Parent(s): 0490f39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -4,7 +4,7 @@ import pandas as pd
4
  import comtradeapicall
5
  from huggingface_hub import InferenceClient
6
 
7
- # کلید COMTRADE (فعلاً برای preview نیازی به آن نداریم)
8
  subscription_key = os.getenv("COMTRADE_API_KEY", "")
9
  # توکن Hugging Face
10
  hf_token = os.getenv("HF_API_TOKEN")
@@ -21,14 +21,14 @@ def get_importers(hs_code: str, year: str, month: str):
21
  maxRecords=500, includeDesc=True
22
  )
23
  if df is None or df.empty:
24
- return pd.DataFrame(columns=["کد کشور","نام کشور","ارزش CIF"])
25
  df = df[df['cifvalue'] > 0]
26
  result = (
27
- df.groupby(["reporterCode","reporterDesc"], as_index=False)
28
- .agg({"cifvalue":"sum"})
29
  .sort_values("cifvalue", ascending=False)
30
  )
31
- result.columns = ["کد کشور","نام کشور","ارزش CIF"]
32
  return result
33
 
34
  def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
@@ -41,14 +41,13 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
41
  f"{table_str}\n\n"
42
  "لطفاً بر اساس این اطلاعات دو پاراگراف مشاوره تخصصی بنویسید."
43
  )
44
- # فراخوانی text_generation
45
  outputs = client.text_generation(
46
  model="google/gemma-2b",
47
- inputs=prompt,
48
- parameters={"max_new_tokens":256}
49
  )
50
- # خروجی اولین شات
51
- return outputs[0]["generated_text"]
52
 
53
  current_year = pd.Timestamp.now().year
54
  years = [str(y) for y in range(2000, current_year+1)]
@@ -57,13 +56,13 @@ months = [str(m) for m in range(1, 13)]
57
  with gr.Blocks() as demo:
58
  gr.Markdown("## نمایش کشورهایی که یک کالا را وارد کرده‌اند")
59
  with gr.Row():
60
- inp_hs = gr.Textbox(label="HS Code")
61
- inp_year = gr.Dropdown(choices=years, label="سال", value=str(current_year))
62
  inp_month = gr.Dropdown(choices=months, label="ماه", value=str(pd.Timestamp.now().month))
63
  btn_show = gr.Button("نمایش اطلاعات")
64
  out_table = gr.Dataframe(
65
- headers=["کد کشور","نام کشور","ارزش CIF"],
66
- datatype=["number","text","number"],
67
  interactive=True,
68
  )
69
  btn_show.click(get_importers, [inp_hs, inp_year, inp_month], out_table)
@@ -78,4 +77,4 @@ with gr.Blocks() as demo:
78
  )
79
 
80
  if __name__ == "__main__":
81
- demo.launch()
 
4
  import comtradeapicall
5
  from huggingface_hub import InferenceClient
6
 
7
+ # کلید COMTRADE
8
  subscription_key = os.getenv("COMTRADE_API_KEY", "")
9
  # توکن Hugging Face
10
  hf_token = os.getenv("HF_API_TOKEN")
 
21
  maxRecords=500, includeDesc=True
22
  )
23
  if df is None or df.empty:
24
+ return pd.DataFrame(columns=["کد کشور", "نام کشور", "ارزش CIF"])
25
  df = df[df['cifvalue'] > 0]
26
  result = (
27
+ df.groupby(["reporterCode", "reporterDesc"], as_index=False)
28
+ .agg({"cifvalue": "sum"})
29
  .sort_values("cifvalue", ascending=False)
30
  )
31
+ result.columns = ["کد کشور", "نام کشور", "ارزش CIF"]
32
  return result
33
 
34
  def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
 
41
  f"{table_str}\n\n"
42
  "لطفاً بر اساس این اطلاعات دو پاراگراف مشاوره تخصصی بنویسید."
43
  )
44
+ # فراخوانی text_generation با پارامتر درست
45
  outputs = client.text_generation(
46
  model="google/gemma-2b",
47
+ prompt=prompt,
48
+ max_new_tokens=256
49
  )
50
+ return outputs
 
51
 
52
  current_year = pd.Timestamp.now().year
53
  years = [str(y) for y in range(2000, current_year+1)]
 
56
  with gr.Blocks() as demo:
57
  gr.Markdown("## نمایش کشورهایی که یک کالا را وارد کرده‌اند")
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=True,
67
  )
68
  btn_show.click(get_importers, [inp_hs, inp_year, inp_month], out_table)
 
77
  )
78
 
79
  if __name__ == "__main__":
80
+ demo.launch()