Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,115 +0,0 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
import os
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import gradio as gr
|
| 5 |
-
import comtradeapicall
|
| 6 |
-
from openai 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"
|
| 14 |
-
)
|
| 15 |
-
hs_df = pd.read_csv(HS_CSV_URL, dtype=str)
|
| 16 |
-
|
| 17 |
-
def get_product_name(hs_code: str) -> str:
|
| 18 |
-
code4 = str(hs_code).zfill(4)
|
| 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}"
|
| 25 |
-
df = comtradeapicall.previewFinalData(
|
| 26 |
-
typeCode='C', freqCode='M', clCode='HS', period=period,
|
| 27 |
-
reporterCode=None, cmdCode=hs_code, flowCode='M',
|
| 28 |
-
partnerCode=None, partner2Code=None,
|
| 29 |
-
customsCode=None, motCode=None,
|
| 30 |
-
maxRecords=500, includeDesc=True
|
| 31 |
-
)
|
| 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 و مترجم ---
|
| 53 |
-
openai_client = OpenAI(api_key=os.getenv("OPENAI")) # سکرت از محیط
|
| 54 |
-
translator = GoogleTranslator(source='en', target='fa')
|
| 55 |
-
|
| 56 |
-
@spaces.GPU
|
| 57 |
-
def provide_advice(table_data: pd.DataFrame, hs_code: str, year: str, month: str):
|
| 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}"
|
| 64 |
-
prompt = (
|
| 65 |
-
f"The following table shows the top {len(df_limited)} countries by CIF value importing HS code {hs_code} during {period}:\n"
|
| 66 |
-
f"{table_str}\n\n"
|
| 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_client.chat.completions.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 |
-
|
| 86 |
-
# --- رابط کاربری Gradio ---
|
| 87 |
-
with gr.Blocks() as demo:
|
| 88 |
-
gr.Markdown("## DIGINORON.COM ابزار هوش مصنوعی برای مشاوره صادرات کالا به کشورهای هدف")
|
| 89 |
-
|
| 90 |
-
with gr.Row():
|
| 91 |
-
inp_hs = gr.Textbox(label="کد HS", placeholder="مثلاً 1006")
|
| 92 |
-
inp_year = gr.Textbox(label="سال", placeholder="مثلاً 2023")
|
| 93 |
-
inp_month = gr.Textbox(label="ماه", placeholder="مثلاً 1 تا 12")
|
| 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,
|
| 101 |
-
inputs=[inp_hs, inp_year, inp_month],
|
| 102 |
-
outputs=[out_name, out_table]
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
btn_advice = gr.Button("ارائه مشاوره تخصصی")
|
| 106 |
-
out_advice = gr.Textbox(label="مشاوره تخصصی", lines=8)
|
| 107 |
-
|
| 108 |
-
btn_advice.click(
|
| 109 |
-
fn=provide_advice,
|
| 110 |
-
inputs=[out_table, inp_hs, inp_year, inp_month],
|
| 111 |
-
outputs=out_advice
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
if __name__ == "__main__":
|
| 115 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|