diginoron commited on
Commit
fd96a70
·
verified ·
1 Parent(s): 809a8dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -28
app.py CHANGED
@@ -33,24 +33,14 @@ def get_importers(hs_code: str, year: str, month: str):
33
  if df is None or df.empty:
34
  return product_name, pd.DataFrame()
35
 
36
- # شناسایی ستون‌های مورد نیاز (کد کشور، نام کشور، ارزش)
37
- # ابتدا سعی در استفاده از ستون‌های استاندارد
38
- std_map = {
39
- 'کد کشور': 'ptCode',
40
- 'نام کشور': 'ptTitle',
41
- 'ارزش CIF': 'TradeValue'
42
- }
43
- code_col = std_map['کد کشور'] if 'ptCode' in df.columns else next((c for c in df.columns if 'code' in c.lower()), None)
44
- title_col= std_map['نام کشور'] if 'ptTitle' in df.columns else next((c for c in df.columns if 'title' in c.lower()), None)
45
- value_col= std_map['ارزش CIF'] if 'TradeValue' in df.columns else next((c for c in df.columns if 'value' in c.lower()), None)
46
-
47
  if not (code_col and title_col and value_col):
48
- # اگر نتوانست ستون‌ها را شناسایی کند، برگرداندن DataFrame خام
49
  return product_name, df
50
 
51
- # محدودسازی به 10 کشور برتر بر اساس ستون value_col
52
  df_sorted = df.sort_values(value_col, ascending=False).head(10)
53
-
54
  out = df_sorted[[code_col, title_col, value_col]]
55
  out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
56
  return product_name, out
@@ -63,31 +53,42 @@ translator = GoogleTranslator(source='en', target='fa')
63
  @spaces.GPU
64
  def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
65
  if table_data is None or table_data.empty:
66
- return "ابتدا نمایش داده‌های واردات را انجام دهید."
67
 
68
- # محدودسازی تعداد ردیف‌های ورودی به 10 (در صورت بیشتر)
69
  df_limited = table_data.head(10)
70
  table_str = df_limited.to_string(index=False)
71
  period = f"{year}/{int(month):02d}"
72
- prompt = (
73
- f"The following table shows the top {len(df_limited)} countries by CIF value importing HS code {hs_code} during {period}:\n"
74
- f"{table_str}\n\n"
75
- "Please provide a detailed and comprehensive analysis of market trends, risks, "
76
- "and opportunities for a new exporter entering this market."
 
 
 
 
 
 
 
 
 
 
77
  )
78
  try:
79
  outputs = client.text_generation(
80
  prompt=prompt,
81
  model="mistralai/Mixtral-8x7B-Instruct-v0.1",
82
- max_new_tokens=1024
83
  )
84
- return translator.translate(outputs)
 
 
85
  except Exception as e:
86
- return f"خطا در تولید مشاوره: {e}"
87
 
88
  # --- رابط کاربری Gradio ---
89
  with gr.Blocks() as demo:
90
- gr.Markdown("## تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی")
91
 
92
  with gr.Row():
93
  inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006")
@@ -95,7 +96,7 @@ with gr.Blocks() as demo:
95
  inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
96
 
97
  btn_show = gr.Button("نمایش داده‌های واردات")
98
- out_name = gr.Markdown(label="**نام محصول**")
99
  out_table = gr.Dataframe(
100
  datatype="pandas",
101
  interactive=True
@@ -108,7 +109,7 @@ with gr.Blocks() as demo:
108
  )
109
 
110
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
111
- out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
112
 
113
  btn_advice.click(
114
  fn=provide_advice,
@@ -117,4 +118,4 @@ with gr.Blocks() as demo:
117
  )
118
 
119
  if __name__ == "__main__":
120
- demo.launch()
 
33
  if df is None or df.empty:
34
  return product_name, pd.DataFrame()
35
 
36
+ # شناسایی ستون‌های مورد نیاز
37
+ code_col = next((c for c in df.columns if 'code' in c.lower()), None)
38
+ title_col = next((c for c in df.columns if 'title' in c.lower()), None)
39
+ value_col = next((c for c in df.columns if 'value' in c.lower()), None)
 
 
 
 
 
 
 
40
  if not (code_col and title_col and value_col):
 
41
  return product_name, df
42
 
 
43
  df_sorted = df.sort_values(value_col, ascending=False).head(10)
 
44
  out = df_sorted[[code_col, title_col, value_col]]
45
  out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
46
  return product_name, out
 
53
  @spaces.GPU
54
  def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
55
  if table_data is None or table_data.empty:
56
+ return "<div dir=\"rtl\" style=\"text-align: right; font-family: IRANSans;\">ابتدا نمایش داده‌های واردات را انجام دهید.</div>"
57
 
 
58
  df_limited = table_data.head(10)
59
  table_str = df_limited.to_string(index=False)
60
  period = f"{year}/{int(month):02d}"
61
+ prompt = f"""
62
+ لطفاً یک گزارش تحلیلی به زبان فارسی و با چینش راست‌به‌چپ تولید کن، شامل دو بخش:
63
+ 1. جدول خلاصه (Markdown) 10 کشور برتر با ستون‌های رتبه، نام کشور، ارزش CIF و سه تأمین‌کننده برتر.
64
+ 2. نکات کلیدی و پیشنهادات به‌صورت بولت‌پوینت با تراز راست.
65
+
66
+ - متن خروجی را درون یک <div dir=\"rtl\" style=\"text-align: right; font-family: IRANSans;\"> قرار بده.
67
+
68
+ داده‌ها:
69
+ {table_str}
70
+ """ 10 کشور برتر با ستون‌های رتبه، نام کشور، ارزش CIF و سه تأمین‌کننده برتر."
71
+ "\n2. نکات کلیدی و پیشنهادات به‌صورت بولت‌پوینت با تراز راست."
72
+ "\n
73
+ - متن خروجی را درون یک <div dir=\\\"rtl\\\" style=\\\"text-align: right; font-family: IRANSans;\\\"> قرار بده."
74
+ f"\n
75
+ داده‌ها:\n{table_str}"
76
  )
77
  try:
78
  outputs = client.text_generation(
79
  prompt=prompt,
80
  model="mistralai/Mixtral-8x7B-Instruct-v0.1",
81
+ max_new_tokens=2048
82
  )
83
+ persian = translator.translate(outputs)
84
+ # قرار دادن در کانتینر HTML راست‌چین
85
+ return f"<div dir=\"rtl\" style=\"text-align: right; font-family: IRANSans;\">{persian}</div>"
86
  except Exception as e:
87
+ return f"<div dir=\"rtl\" style=\"text-align: right; font-family: IRANSans;\">خطا در تولید مشاوره: {e}</div>"
88
 
89
  # --- رابط کاربری Gradio ---
90
  with gr.Blocks() as demo:
91
+ gr.Markdown("<div dir=\"rtl\" style=\"text-align: right; font-family: IRANSans;\"><h2>تحلیل واردات بر اساس کد HS و ارائه مشاوره تخصصی</h2></div>", elem_id="title")
92
 
93
  with gr.Row():
94
  inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006")
 
96
  inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
97
 
98
  btn_show = gr.Button("نمایش داده‌های واردات")
99
+ out_name = gr.Markdown(label="**<div dir=\"rtl\" style=\"text-align: right; font-family: IRANSans;\">نام محصول</div>")
100
  out_table = gr.Dataframe(
101
  datatype="pandas",
102
  interactive=True
 
109
  )
110
 
111
  btn_advice = gr.Button("ارائه مشاوره تخصصی")
112
+ out_advice = gr.HTML()
113
 
114
  btn_advice.click(
115
  fn=provide_advice,
 
118
  )
119
 
120
  if __name__ == "__main__":
121
+ demo.launch()