File size: 3,246 Bytes
83a2a70
3ba690a
 
 
83a2a70
 
 
 
 
3ba690a
c62298d
83a2a70
 
 
 
3ba690a
e5574dd
 
3ba690a
 
 
 
 
 
 
 
 
5ead8fb
3ba690a
99af9f6
 
 
 
 
 
c62298d
3ba690a
83a2a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5574dd
 
 
3ba690a
99af9f6
3ba690a
e5574dd
 
83a2a70
 
 
99af9f6
c62298d
83a2a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c62298d
3ba690a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import gradio as gr
import pandas as pd
import comtradeapicall
from huggingface_hub import InferenceApi

# Environment variables:
# COMTRADE_API_KEY  -> برای COMTRADE (preview نیازی نیست اما برای آینده)
# HF_API_TOKEN     -> برای Hugging Face Inference API

subscription_key = os.getenv("COMTRADE_API_KEY", "")
hf_token         = os.getenv("HF_API_TOKEN", None)
proxy_url        = None

inference = InferenceApi(repo_id="google/gemma-2b", token=hf_token)

def get_importers(hs_code: str, year: str, month: str):
    period = f"{year}{int(month):02d}"
    df = comtradeapicall.previewFinalData(
        typeCode='C', freqCode='M', clCode='HS', period=period,
        reporterCode=None, cmdCode=hs_code, flowCode='M',
        partnerCode=None, partner2Code=None,
        customsCode=None, motCode=None,
        maxRecords=500, includeDesc=True,
        proxy_url=proxy_url
    )
    if df is None or df.empty:
        return pd.DataFrame(columns=["کد کشور","نام کشور","ارزش CIF"])
    df = df[df['cifvalue'] > 0]
    result = (
        df.groupby(["reporterCode","reporterDesc"], as_index=False)
          .agg({"cifvalue":"sum"})
          .sort_values("cifvalue", ascending=False)
    )
    result.columns = ["کد کشور","نام کشور","ارزش CIF"]
    return result

def provide_advice(table, hs_code: str, year: str, month: str):
    if not table:
        return "ابتدا باید اطلاعات واردات را نمایش دهید."
    df = pd.DataFrame(table, columns=["کد کشور","نام کشور","ارزش CIF"])
    table_str = df.to_markdown(index=False)
    period = f"{year}/{int(month):02d}"
    prompt = (
        f"جدول زیر اطلاعات کشورهایی است که کالا با کد HS {hs_code} را در دوره {period} وارد کرده‌اند:\n"
        f"{table_str}\n\n"
        "لطفاً بر اساس این اطلاعات دو پاراگراف مشاوره تخصصی بنویسید."
    )
    response = inference(prompt)
    return response

current_year = pd.Timestamp.now().year
years = [str(y) for y in range(2000, current_year+1)]
months = [str(m) for m in range(1, 13)]

with gr.Blocks() as demo:
    gr.Markdown("## نمایش کشورهایی که یک کالا را وارد کرده‌اند")
    with gr.Row():
        inp_hs    = gr.Textbox(label="HS Code")
        inp_year  = gr.Dropdown(choices=years, label="سال", value=str(current_year))
        inp_month = gr.Dropdown(choices=months, label="ماه", value=str(pd.Timestamp.now().month))
    btn_show = gr.Button("نمایش اطلاعات")
    out_table = gr.Dataframe(
        headers=["کد کشور","نام کشور","ارزش CIF"],
        datatype=["number","text","number"],
        interactive=False,
    )
    btn_show.click(
        fn=get_importers,
        inputs=[inp_hs, inp_year, inp_month],
        outputs=out_table
    )

    btn_advice = gr.Button("ارائه مشاوره تخصصی")
    out_advice = gr.Textbox(label="مشاوره تخصصی")

    btn_advice.click(
        fn=provide_advice,
        inputs=[out_table, inp_hs, inp_year, inp_month],
        outputs=out_advice
    )

if __name__ == "__main__":
    demo.launch()