Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,11 @@ import os
|
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
import comtradeapicall
|
| 6 |
-
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
import spaces # برای مدیریت GPU کرایهای
|
| 9 |
|
| 10 |
-
# --- بارگذاری
|
| 11 |
HS_CSV_URL = (
|
| 12 |
"https://raw.githubusercontent.com/"
|
| 13 |
"datasets/harmonized-system/master/data/harmonized-system.csv"
|
|
@@ -19,7 +19,6 @@ def get_product_name(hs_code: str) -> str:
|
|
| 19 |
row = hs_df[hs_df["hscode"] == code4]
|
| 20 |
return row.iloc[0]["description"] if not row.empty else "–"
|
| 21 |
|
| 22 |
-
# --- تابع دریافت واردات و پردازش ستونها ---
|
| 23 |
def get_importers(hs_code: str, year: str, month: str):
|
| 24 |
product_name = get_product_name(hs_code)
|
| 25 |
period = f"{year}{int(month):02d}"
|
|
@@ -33,31 +32,25 @@ 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
|
| 57 |
|
| 58 |
-
# ---
|
| 59 |
-
|
| 60 |
-
client = InferenceClient(token=hf_token)
|
| 61 |
translator = GoogleTranslator(source='en', target='fa')
|
| 62 |
|
| 63 |
@spaces.GPU
|
|
@@ -65,7 +58,6 @@ 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}"
|
|
@@ -75,13 +67,19 @@ def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str
|
|
| 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 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
-
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
return f"خطا در تولید مشاوره: {e}"
|
| 87 |
|
|
@@ -96,10 +94,7 @@ with gr.Blocks() as demo:
|
|
| 96 |
|
| 97 |
btn_show = gr.Button("نمایش دادههای واردات")
|
| 98 |
out_name = gr.Markdown(label="**نام محصول**")
|
| 99 |
-
out_table = gr.Dataframe(
|
| 100 |
-
datatype="pandas",
|
| 101 |
-
interactive=True
|
| 102 |
-
)
|
| 103 |
|
| 104 |
btn_show.click(
|
| 105 |
fn=get_importers,
|
|
@@ -117,4 +112,4 @@ with gr.Blocks() as demo:
|
|
| 117 |
)
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
-
demo.launch()
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
import comtradeapicall
|
| 6 |
+
import openai
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
import spaces # برای مدیریت GPU کرایهای
|
| 9 |
|
| 10 |
+
# --- بارگذاری دادههای HS Code ---
|
| 11 |
HS_CSV_URL = (
|
| 12 |
"https://raw.githubusercontent.com/"
|
| 13 |
"datasets/harmonized-system/master/data/harmonized-system.csv"
|
|
|
|
| 19 |
row = hs_df[hs_df["hscode"] == code4]
|
| 20 |
return row.iloc[0]["description"] if not row.empty else "–"
|
| 21 |
|
|
|
|
| 22 |
def get_importers(hs_code: str, year: str, month: str):
|
| 23 |
product_name = get_product_name(hs_code)
|
| 24 |
period = f"{year}{int(month):02d}"
|
|
|
|
| 32 |
if df is None or df.empty:
|
| 33 |
return product_name, pd.DataFrame()
|
| 34 |
|
|
|
|
|
|
|
| 35 |
std_map = {
|
| 36 |
'کد کشور': 'ptCode',
|
| 37 |
'نام کشور': 'ptTitle',
|
| 38 |
'ارزش CIF': 'TradeValue'
|
| 39 |
}
|
| 40 |
code_col = std_map['کد کشور'] if 'ptCode' in df.columns else next((c for c in df.columns if 'code' in c.lower()), None)
|
| 41 |
+
title_col = std_map['نام کشور'] if 'ptTitle' in df.columns else next((c for c in df.columns if 'title' in c.lower()), None)
|
| 42 |
+
value_col = std_map['ارزش CIF'] if 'TradeValue' in df.columns else next((c for c in df.columns if 'value' in c.lower()), None)
|
| 43 |
|
| 44 |
if not (code_col and title_col and value_col):
|
|
|
|
| 45 |
return product_name, df
|
| 46 |
|
|
|
|
| 47 |
df_sorted = df.sort_values(value_col, ascending=False).head(10)
|
|
|
|
| 48 |
out = df_sorted[[code_col, title_col, value_col]]
|
| 49 |
out.columns = ['کد کشور', 'نام کشور', 'ارزش CIF']
|
| 50 |
return product_name, out
|
| 51 |
|
| 52 |
+
# --- اتصال به OpenAI با کلید سکرت HuggingFace ---
|
| 53 |
+
openai.api_key = os.getenv("OPENAI") # استفاده از سکرت تعریفشده در فضای HF
|
|
|
|
| 54 |
translator = GoogleTranslator(source='en', target='fa')
|
| 55 |
|
| 56 |
@spaces.GPU
|
|
|
|
| 58 |
if table_data is None or table_data.empty:
|
| 59 |
return "ابتدا نمایش دادههای واردات را انجام دهید."
|
| 60 |
|
|
|
|
| 61 |
df_limited = table_data.head(10)
|
| 62 |
table_str = df_limited.to_string(index=False)
|
| 63 |
period = f"{year}/{int(month):02d}"
|
|
|
|
| 67 |
"Please provide a detailed and comprehensive analysis of market trends, risks, "
|
| 68 |
"and opportunities for a new exporter entering this market."
|
| 69 |
)
|
| 70 |
+
|
| 71 |
try:
|
| 72 |
+
response = openai.ChatCompletion.create(
|
| 73 |
+
model="gpt-3.5-turbo",
|
| 74 |
+
messages=[
|
| 75 |
+
{"role": "system", "content": "You are an expert in international trade and export consulting."},
|
| 76 |
+
{"role": "user", "content": prompt}
|
| 77 |
+
],
|
| 78 |
+
max_tokens=1000,
|
| 79 |
+
temperature=0.7
|
| 80 |
)
|
| 81 |
+
english_response = response['choices'][0]['message']['content']
|
| 82 |
+
return translator.translate(english_response)
|
| 83 |
except Exception as e:
|
| 84 |
return f"خطا در تولید مشاوره: {e}"
|
| 85 |
|
|
|
|
| 94 |
|
| 95 |
btn_show = gr.Button("نمایش دادههای واردات")
|
| 96 |
out_name = gr.Markdown(label="**نام محصول**")
|
| 97 |
+
out_table = gr.Dataframe(datatype="pandas", interactive=True)
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
btn_show.click(
|
| 100 |
fn=get_importers,
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
+
demo.launch()
|