diginoron commited on
Commit
74a942e
·
verified ·
1 Parent(s): e5eed83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -23,7 +23,7 @@ def get_importers(hs_code: str, year: str, month: str):
23
  maxRecords=500, includeDesc=True
24
  )
25
  if df is None or df.empty:
26
- return pd.DataFrame(columns=["کد کشور", "نام کشور", "ارزش CIF"])
27
  df = df[df['cifvalue'] > 0]
28
  result = (
29
  df.groupby(["reporterCode", "reporterDesc"], as_index=False)
@@ -31,26 +31,28 @@ def get_importers(hs_code: str, year: str, month: str):
31
  .sort_values("cifvalue", ascending=False)
32
  )
33
  result.columns = ["کد کشور", "نام کشور", "ارزش CIF"]
34
- return result
 
 
35
 
36
- def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
37
  if table_data is None or table_data.empty:
38
  return "ابتدا باید اطلاعات واردات را نمایش دهید."
39
  table_str = table_data.to_string(index=False)
40
  period = f"{year}/{int(month):02d}"
41
  prompt = (
42
- f"The following table shows countries that imported a product with HS code {hs_code} during the period {period}:\n"
43
  f"{table_str}\n\n"
44
- f"Please provide a detailed and comprehensive analysis in two paragraphs. The first paragraph should discuss market opportunities, potential demand, and specific cultural or economic factors influencing the demand for this product in these countries. The second paragraph should offer actionable strategic recommendations for exporters, including detailed trade strategies, risk management techniques, and steps to establish local partnerships."
45
  )
46
  print("پرامپت ساخته‌شده:")
47
  print(prompt)
48
  try:
49
- print("در حال فراخوانی مدل mistralai/Mixtral-8x7B-Instruct-v0.1...")
50
  outputs = client.text_generation(
51
  prompt=prompt,
52
- model="mistralai/Mixtral-8x7B-Instruct-v0.1",
53
- max_new_tokens=1024 # افزایش برای تکمیل جملات
54
  )
55
  print("خروجی مدل دریافت شد (به انگلیسی):")
56
  print(outputs)
@@ -72,7 +74,7 @@ months = [str(m) for m in range(1, 13)]
72
  with gr.Blocks() as demo:
73
  gr.Markdown("## نمایش کشورهایی که یک کالا را وارد کرده‌اند")
74
  with gr.Row():
75
- inp_hs = gr.Textbox(label="HS Code")
76
  inp_year = gr.Dropdown(choices=years, label="سال", value=str(current_year))
77
  inp_month = gr.Dropdown(choices=months, label="ماه", value=str(pd.Timestamp.now().month))
78
  btn_show = gr.Button("نمایش اطلاعات")
@@ -81,14 +83,18 @@ with gr.Blocks() as demo:
81
  datatype=["number", "text", "number"],
82
  interactive=True,
83
  )
84
- btn_show.click(get_importers, [inp_hs, inp_year, inp_month], out_table)
 
 
 
 
85
 
86
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
87
  out_advice = gr.Textbox(label="مشاوره تخصصی", lines=6)
88
 
89
  btn_advice.click(
90
- provide_advice,
91
- inputs=[out_table, inp_hs, inp_year, inp_month],
92
  outputs=out_advice
93
  )
94
 
 
23
  maxRecords=500, includeDesc=True
24
  )
25
  if df is None or df.empty:
26
+ return pd.DataFrame(columns=["کد کشور", "نام کشور", "ارزش CIF"]), "برنج"
27
  df = df[df['cifvalue'] > 0]
28
  result = (
29
  df.groupby(["reporterCode", "reporterDesc"], as_index=False)
 
31
  .sort_values("cifvalue", ascending=False)
32
  )
33
  result.columns = ["کد کشور", "نام کشور", "ارزش CIF"]
34
+
35
+ product_name = df['cmdDesc'].iloc[0] if 'cmdDesc' in df.columns else "برنج"
36
+ return result, product_name
37
 
38
+ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str, product_name: str):
39
  if table_data is None or table_data.empty:
40
  return "ابتدا باید اطلاعات واردات را نمایش دهید."
41
  table_str = table_data.to_string(index=False)
42
  period = f"{year}/{int(month):02d}"
43
  prompt = (
44
+ f"The following table shows countries that imported the product '{product_name}' with HS code {hs_code} during the period {period}:\n"
45
  f"{table_str}\n\n"
46
+ f"لطفاً یک تحلیل کامل ارائه دهید که شامل دو بخش باشد. بخش اول فرصت‌های بازار و تقاضای بالقوه برای این محصول در این کشورها را بررسی کند، با توجه به عوامل فرهنگی، اقتصادی و جمعیتی. بخش دوم توصیه‌های استراتژیک عملی برای صادرکنندگان که این بازارها را هدف قرار داده‌اند، با تمرکز بر استراتژی‌های تجاری، مدیریت ریسک و ایجاد مشارکت‌های محلی، ارائه دهد."
47
  )
48
  print("پرامپت ساخته‌شده:")
49
  print(prompt)
50
  try:
51
+ print("در حال فراخوانی مدل google/gemma-2b-it...")
52
  outputs = client.text_generation(
53
  prompt=prompt,
54
+ model="google/gemma-2b-it", # مدل سبک‌تر
55
+ max_new_tokens=1024
56
  )
57
  print("خروجی مدل دریافت شد (به انگلیسی):")
58
  print(outputs)
 
74
  with gr.Blocks() as demo:
75
  gr.Markdown("## نمایش کشورهایی که یک کالا را وارد کرده‌اند")
76
  with gr.Row():
77
+ inp_hs = gr.Textbox(label="HS Code", value="1006")
78
  inp_year = gr.Dropdown(choices=years, label="سال", value=str(current_year))
79
  inp_month = gr.Dropdown(choices=months, label="ماه", value=str(pd.Timestamp.now().month))
80
  btn_show = gr.Button("نمایش اطلاعات")
 
83
  datatype=["number", "text", "number"],
84
  interactive=True,
85
  )
86
+ btn_show.click(
87
+ fn=get_importers,
88
+ inputs=[inp_hs, inp_year, inp_month],
89
+ outputs=[out_table, gr.State()]
90
+ )
91
 
92
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
93
  out_advice = gr.Textbox(label="مشاوره تخصصی", lines=6)
94
 
95
  btn_advice.click(
96
+ fn=provide_advice,
97
+ inputs=[out_table, inp_hs, inp_year, inp_month, gr.State()],
98
  outputs=out_advice
99
  )
100