Download app.py from Sangwon78/Test3: direct link, hf CLI and curl.
- Browser
- Download file 15 kB
-
https://huggingface.co/spaces/Sangwon78/Test3/resolve/main/app.py
- Command line
-
hf download hf://spaces/Sangwon78/Test3/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Sangwon78/Test3/resolve/main/app.py
15 kB
| import os | |
| import openai | |
| import gradio as gr | |
| ################################### | |
| # (ํ์) OpenAI API ํด๋ผ์ด์ธํธ ์ค์ | |
| ################################### | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| ################################### | |
| # [api์๋ฃ]์์ ์ฃผ์ด์ง ํจ์ (์์ ๊ธ์ง)import os | |
| import openai | |
| import gradio as gr | |
| from openpyxl import load_workbook | |
| from io import BytesIO | |
| import tempfile | |
| ################################### | |
| # (ํ์) OpenAI API ํด๋ผ์ด์ธํธ ์ค์ | |
| ################################### | |
| # ๊ณต๋ฐฑ/๊ฐํ์ด ์์ผ๋ฉด strip() ์ฒ๋ฆฌ | |
| openai.api_key = os.getenv("OPENAI_API_KEY").strip() | |
| ################################### | |
| # [api์๋ฃ]์์ ์ฃผ์ด์ง ํจ์ (์์ ๊ธ์ง) | |
| ################################### | |
| def call_api(content, system_message, max_tokens, temperature, top_p): | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o-mini", # gpt-4o-mini ๋ชจ๋ธ ์ฌ์ฉ | |
| messages=[ | |
| {"role": "system", "content": system_message}, | |
| {"role": "user", "content": content}, | |
| ], | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ) | |
| return response.choices[0].message['content'] | |
| ################################### | |
| # (1) ์ฌ๋ฌด ๋ถ์ ํจ์ | |
| ################################### | |
| def analyze_financials( | |
| avg_monthly_sales, # ์ํ๊ท ๋งค์ถ์ก | |
| rent, # ์์ฐจ๋ฃ | |
| labor_cost, # ์ธ๊ฑด๋น | |
| management_cost, # ๊ด๋ฆฌ๋น | |
| utilities, # ์๋๊ด์ด๋น | |
| current_interest, # ์ง๊ธ์ด์ | |
| other_cost, # ๊ธฐํ๋น์ฉ | |
| cost_ratio # ์๊ฐ์จ (์ถ๋ ฅ์๋ ํฌํจ ์ ํจ) | |
| ): | |
| """ | |
| ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ 8๊ฐ์ง ํญ๋ชฉ(์ํ๊ท ๋งค์ถ์ก, ์์ฐจ๋ฃ, ์ธ๊ฑด๋น, ๊ด๋ฆฌ๋น, ์๋๊ด์ด๋น, ์ง๊ธ์ด์, ๊ธฐํ๋น์ฉ, ์๊ฐ์จ)์ ๋ฐํ์ผ๋ก | |
| ๊ฐ๊ฐ์๊ฐ๋น, ์์ด์ต์จ, ์์์์ต, ์์ต๋ถ๊ธฐ์ ์ ๊ณ์ฐ(๋๋ ์ถ์ )ํ์ฌ, | |
| ๊ฐ ํญ๋ชฉ๋ณ '์ ๋ชฉ: ์์น' ํํ๋ก๋ง ๋ฐํํ๋ ํจ์ | |
| (์ถ๋ ฅ์์ ์๊ฐ์จ์ ์ ์ธ) | |
| """ | |
| system_message = ( | |
| "๋น์ ์ ์ธ๋ฌด์ฌ(ํ๊ณ์ฌ)์ ๋๋ค. ์๋ ์ ๋ ฅ๊ฐ์ ๋ฐํ์ผ๋ก ๊ณ์ฐํ ๊ฐ๊ฐ์๊ฐ๋น, ์์ด์ต์จ, " | |
| "์์์์ต, ์์ต๋ถ๊ธฐ์ ์ '์ ๋ชฉ: ์์น' ํ์์ผ๋ก๋ง ์๋ ค์ฃผ์ธ์. " | |
| "์ถ๊ฐ ์ค๋ช ์ด๋ ๋ฌธ์ฅ ์์ด, ํญ๋ชฉ๋ช ๊ณผ ์์น๋ง ๋ฐํํด์ฃผ์ธ์." | |
| ) | |
| content = f""" | |
| ์ ๋ ฅ๋ ๋ฐ์ดํฐ: | |
| - ์ํ๊ท ๋งค์ถ์ก: {avg_monthly_sales} | |
| - ์์ฐจ๋ฃ: {rent} | |
| - ์ธ๊ฑด๋น: {labor_cost} | |
| - ๊ด๋ฆฌ๋น: {management_cost} | |
| - ์๋๊ด์ด๋น: {utilities} | |
| - ์ง๊ธ์ด์: {current_interest} | |
| - ๊ธฐํ๋น์ฉ: {other_cost} | |
| - ์๊ฐ์จ: {cost_ratio} | |
| ๋ค์ ํญ๋ชฉ์ ๊ฒฐ๊ณผ๋ฅผ '์ ๋ชฉ: ์์น' ํํ๋ก๋ง ๋ฐํํด ์ฃผ์ธ์: | |
| 1) ๊ฐ๊ฐ์๊ฐ๋น | |
| 2) ์์ด์ต์จ | |
| 3) ์์์์ต | |
| 4) ์์ต๋ถ๊ธฐ์ | |
| """ | |
| return call_api( | |
| content=content, | |
| system_message=system_message, | |
| max_tokens=1024, | |
| temperature=0.7, | |
| top_p=1.0 | |
| ) | |
| ################################### | |
| # (2) SWOT ๋ถ์ ํจ์ | |
| ################################### | |
| def analyze_swot(strengths, weaknesses, opportunities, threats): | |
| """ | |
| ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๊ฐ์ , ์ฝ์ , ๊ธฐํ, ์ํ์ ๋ํด | |
| gpt-4o-mini ๋ชจ๋ธ์ ํ์ฉํด ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ํจ์ | |
| """ | |
| system_message = ( | |
| "๋น์ ์ swot ๋ถ์ ์ ๋ฌธ๊ฐ์ ๋๋ค. ์ฌ์ฉ์์ ๊ฐ์ , ์ฝ์ , ๊ธฐํ, ์ํ์ ๋ํด " | |
| "๊ฒ์ํ ๋ด์ฉ์ ์์ฝํ์ฌ ์ ๊ณตํฉ๋๋ค." | |
| ) | |
| content = f""" | |
| ๊ฐ์ : {strengths} | |
| ์ฝ์ : {weaknesses} | |
| ๊ธฐํ: {opportunities} | |
| ์ํ: {threats} | |
| """ | |
| return call_api( | |
| content=content, | |
| system_message=system_message, | |
| max_tokens=1024, | |
| temperature=0.7, | |
| top_p=1.0 | |
| ) | |
| ################################### | |
| # (3) ์๊ถ ๋ถ์ ํจ์ (6์ค ์ด๋ด) | |
| ################################### | |
| def analyze_market(region, industry): | |
| """ | |
| ์ง์ญ, ์ ์ข ์ ์ ๋ ฅ๋ฐ์ 1km ์ด๋ด ์๊ถ๋ถ์์ '์ฌ์ง ์ธ๊ธ'๊ณผ ํจ๊ป, | |
| 6์ค ์ด๋ด ํ ์คํธ๋ก ์์ฝํด ์ฃผ๋ ํจ์ | |
| """ | |
| system_message = ( | |
| "๋น์ ์ ์๊ถ๋ถ์ ์ ๋ฌธ๊ฐ์ ๋๋ค. ์ ๋ ฅ๋ ์ง์ญ๊ณผ ์ ์ข ์ ๋ํด 1km ์ด๋ด ์๊ถ์ ๋ถ์ํด ์ฃผ์ธ์. " | |
| "์ฌ์ง(๋๋ ์๊ฐ์๋ฃ)์ ๋ํ ์ธ๊ธ ํฌํจ, ์๋ต์ 6์ค ์ด๋ด๋ก ์ ํํ์ธ์." | |
| ) | |
| content = f""" | |
| ์ง์ญ: {region} | |
| ์ ์ข : {industry} | |
| """ | |
| return call_api( | |
| content=content, | |
| system_message=system_message, | |
| max_tokens=300, # ๋ต๋ณ ๊ธธ์ด ์ ํ | |
| temperature=0.7, | |
| top_p=1.0 | |
| ) | |
| ################################### | |
| # (4) ์ ๋ก์ฌํญ & ์ปจ์คํ ๋ชฉํ ๋ถ์ ํจ์ | |
| ################################### | |
| def analyze_texts(difficulties, goals): | |
| """ | |
| ์ฌ์ฉ์ ์ ๋ ฅ(์ ๋ก์ฌํญ, ์ปจ์คํ ๋ชฉํ)์ gpt-4o-mini๋ก ๋ถ์ ํ ๊ฒฐ๊ณผ(๋ฏธ๋ฆฌ๋ณด๊ธฐ)๋ฅผ ๋ฆฌํด | |
| """ | |
| # ์ ๋ก์ฌํญ ๋ถ์ | |
| preview_difficulties = call_api( | |
| content=difficulties, | |
| system_message="์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ ๋ก์ฌํญ์ ๊ฐ๋จํ ๋ถ์ ๋ฐ ํด๊ฒฐ๋ฐฉ์ ์ ์ํด์ค.", | |
| max_tokens=50, | |
| temperature=0.7, | |
| top_p=0.9 | |
| ) | |
| # ์ปจ์คํ ๋ชฉํ ๋ถ์ | |
| preview_goals = call_api( | |
| content=goals, | |
| system_message="์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ปจ์คํ ๋ชฉํ์ ๋ํ ์์ฝ ๋ฐ ์คํ๋ฐฉ์์ ์ ์ํด์ค.", | |
| max_tokens=50, | |
| temperature=0.7, | |
| top_p=0.9 | |
| ) | |
| return preview_difficulties, preview_goals | |
| ################################### | |
| # ์์ ์์ฑ(๋ค์ด๋ก๋) ํจ์ | |
| ################################### | |
| def generate_excel(preview_difficulties, preview_goals): | |
| """ | |
| 1) 'suu.xlsx'๋ฅผ ๋ถ๋ฌ์์ | |
| 2) A4 = ์ ๋ก์ฌํญ ๋ถ์ ๊ฒฐ๊ณผ | |
| A5 = ์ปจ์คํ ๋ชฉํ ๋ถ์ ๊ฒฐ๊ณผ | |
| 3) NamedTemporaryFile๋ก ์ ์ฅ ํ ํด๋น ํ์ผ ๊ฒฝ๋ก ๋ฐํ | |
| """ | |
| wb = load_workbook("suu.xlsx") | |
| ws = wb.active | |
| # A4, A5 ์ ์ ๊ฒฐ๊ณผ ๊ธฐ์ | |
| ws["A4"] = preview_difficulties | |
| ws["A5"] = preview_goals | |
| # ๋ฉ๋ชจ๋ฆฌ ๋ฒํผ์ ์ ์ฅ ํ ์์ํ์ผ๋ก ์์ฑ | |
| output = BytesIO() | |
| wb.save(output) | |
| output.seek(0) | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp: | |
| tmp.write(output.getvalue()) | |
| tmp_path = tmp.name | |
| return tmp_path | |
| ################################### | |
| # Gradio ์ด๋ฒคํธ ํจ์๋ค (์ ๋ก์ฌํญ & ๋ชฉํ) | |
| ################################### | |
| def run_analyze(difficulties, goals): | |
| """ | |
| '๋ถ์ํ๊ธฐ' ๋ฒํผ ๋๋ฅด๋ฉด ์คํ: | |
| - gpt-4o-mini API๋ก ๋ถ์ ํ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ํ ์คํธ๋ฅผ ๋ฐํ | |
| """ | |
| preview_difficulties, preview_goals = analyze_texts(difficulties, goals) | |
| return preview_difficulties, preview_goals | |
| def run_generate_excel(difficulties, goals, preview_difficulties, preview_goals): | |
| """ | |
| '์์ ๋ก ๋ค์ด๋ก๋' ๋ฒํผ ๋๋ฅด๋ฉด ์คํ: | |
| - generate_excel()๋ก ์์ ํ์ผ์ ๋ง๋ค๊ณ , | |
| - Gradio File ์ปดํฌ๋ํธ๋ก ๋ค์ด๋ก๋ | |
| """ | |
| excel_path = generate_excel(preview_difficulties, preview_goals) | |
| return excel_path | |
| ################################### | |
| # Gradio ์ฑ ๊ตฌ์ฑ (4๊ฐ ํญ) | |
| ################################### | |
| def create_app(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ํตํฉ Demo") | |
| with gr.Tabs(): | |
| # ========== ํญ1: ์ฌ๋ฌด ๋ถ์ ========== | |
| with gr.Tab("์ฌ๋ฌด ๋ถ์"): | |
| gr.Markdown("### (์ ๋ ฅ์ ์๊ฐ์จ ํฌํจ, ๊ฒฐ๊ณผ์์ ์๊ฐ์จ ์ ์ธ)") | |
| avg_monthly_sales_input = gr.Textbox(label="์ํ๊ท ๋งค์ถ์ก") | |
| rent_input = gr.Textbox(label="์์ฐจ๋ฃ") | |
| labor_cost_input = gr.Textbox(label="์ธ๊ฑด๋น") | |
| management_cost_input = gr.Textbox(label="๊ด๋ฆฌ๋น") | |
| utilities_input = gr.Textbox(label="์๋๊ด์ด๋น") | |
| current_interest_input = gr.Textbox(label="์ง๊ธ์ด์") | |
| other_cost_input = gr.Textbox(label="๊ธฐํ๋น์ฉ") | |
| cost_ratio_input = gr.Textbox(label="์๊ฐ์จ") | |
| result_output_fin = gr.Textbox(label="๊ฒฐ๊ณผ(์ ๋ชฉ: ์์น)") | |
| analyze_button_fin = gr.Button("๊ณ์ฐํ๊ธฐ") | |
| analyze_button_fin.click( | |
| fn=analyze_financials, | |
| inputs=[ | |
| avg_monthly_sales_input, | |
| rent_input, | |
| labor_cost_input, | |
| management_cost_input, | |
| utilities_input, | |
| current_interest_input, | |
| other_cost_input, | |
| cost_ratio_input | |
| ], | |
| outputs=result_output_fin | |
| ) | |
| # ========== ํญ2: SWOT ๋ถ์ ========== | |
| with gr.Tab("SWOT ๋ถ์"): | |
| gr.Markdown("### (๊ฐ์ , ์ฝ์ , ๊ธฐํ, ์ํ)") | |
| strengths_input = gr.Textbox(label="๊ฐ์ ") | |
| weaknesses_input = gr.Textbox(label="์ฝ์ ") | |
| opportunities_input = gr.Textbox(label="๊ธฐํ") | |
| threats_input = gr.Textbox(label="์ํ") | |
| result_output_swot = gr.Textbox(label="๊ฒ์ ๊ฒฐ๊ณผ") | |
| analyze_button_swot = gr.Button("๋ถ์ํ๊ธฐ") | |
| analyze_button_swot.click( | |
| fn=analyze_swot, | |
| inputs=[strengths_input, weaknesses_input, opportunities_input, threats_input], | |
| outputs=result_output_swot | |
| ) | |
| # ========== ํญ3: ์๊ถ ๋ถ์ ========== | |
| with gr.Tab("์๊ถ ๋ถ์"): | |
| gr.Markdown("### (์ง์ญ + ์ ์ข โ 1km ์ด๋ด ์๊ถ๋ถ์, 6์ค ์ด๋ด)") | |
| region_input = gr.Textbox(label="์ง์ญ") | |
| industry_input = gr.Textbox(label="์ ์ข ") | |
| result_output_market = gr.Textbox(label="์๊ถ๋ถ์ ๊ฒฐ๊ณผ (์ต๋ 6์ค)") | |
| analyze_button_market = gr.Button("๋ถ์ํ๊ธฐ") | |
| analyze_button_market.click( | |
| fn=analyze_market, | |
| inputs=[region_input, industry_input], | |
| outputs=result_output_market | |
| ) | |
| # ========== ํญ4: ์ ๋ก์ฌํญ & ๋ชฉํ ๋ถ์ + Excel ========== | |
| with gr.Tab("์ ๋ก์ฌํญ & ๋ชฉํ"): | |
| gr.Markdown("### (์ ๋ก์ฌํญ ๋ฐ ์ปจ์คํ ๋ชฉํ ์ ๋ ฅ -> gpt-4o-mini ๋ถ์ -> Excel ๋ค์ด๋ก๋)") | |
| with gr.Row(): | |
| difficulties_input = gr.Textbox(label="์ ๋ก์ฌํญ ์ ๋ ฅ", lines=3, value="") | |
| goals_input = gr.Textbox(label="์ปจ์คํ ๋ชฉํ ์ ๋ ฅ", lines=3, value="") | |
| analyze_button = gr.Button("๋ถ์ํ๊ธฐ") | |
| preview_difficulties_output = gr.Textbox(label="์ ๋ก์ฌํญ ๋ถ์ ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ", lines=3) | |
| preview_goals_output = gr.Textbox(label="์ปจ์คํ ๋ชฉํ ๋ถ์ ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ", lines=3) | |
| download_button = gr.Button("์์ ๋ก ๋ค์ด๋ก๋") | |
| download_file = gr.File(label="๋ค์ด๋ก๋(ํด๋ฆญ ํ ์ ์ฅ)") | |
| # ๋ถ์ ๋ฒํผ ํด๋ฆญ -> ๋ฏธ๋ฆฌ๋ณด๊ธฐ | |
| analyze_button.click( | |
| fn=run_analyze, | |
| inputs=[difficulties_input, goals_input], | |
| outputs=[preview_difficulties_output, preview_goals_output] | |
| ) | |
| # ๋ค์ด๋ก๋ ๋ฒํผ ํด๋ฆญ -> Excel ์์ฑ | |
| download_button.click( | |
| fn=run_generate_excel, | |
| inputs=[difficulties_input, goals_input, preview_difficulties_output, preview_goals_output], | |
| outputs=[download_file] | |
| ) | |
| demo.launch() | |
| return demo | |
| ################################### | |
| # ์คํ | |
| ################################### | |
| if __name__ == "__main__": | |
| app = create_app() | |
| app.launch() | |
| ################################### | |
| def call_api(content, system_message, max_tokens, temperature, top_p): | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o-mini", # ๋๋ ๋ค๋ฅธ ๋ชจ๋ธ ID ์ฌ์ฉ | |
| messages=[ | |
| {"role": "system", "content": system_message}, | |
| {"role": "user", "content": content}, | |
| ], | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ) | |
| return response.choices[0].message['content'] | |
| ################################### | |
| # ๋ถ์ ํจ์ | |
| ################################### | |
| def analyze_financials( | |
| avg_monthly_sales, # ์ํ๊ท ๋งค์ถ์ก | |
| rent, # ์์ฐจ๋ฃ | |
| labor_cost, # ์ธ๊ฑด๋น | |
| management_cost, # ๊ด๋ฆฌ๋น | |
| utilities, # ์๋๊ด์ด๋น | |
| current_interest, # ์ง๊ธ์ด์ | |
| other_cost # ๊ธฐํ๋น์ฉ | |
| ): | |
| """ | |
| 7๊ฐ์ง ํญ๋ชฉ์ ์ ๋ ฅ๋ฐ์ ๊ฐ๊ฐ์๊ฐ๋น, ์๊ฐ์จ, ์์ด์ต์จ, ์์์์ต, ์์ต๋ถ๊ธฐ์ ์ | |
| '์ ๋ชฉ: ์์น' ํํ๋ก 5์ค๋ง ๋ฐํํ๋ ํจ์. | |
| """ | |
| system_message = ( | |
| "๋น์ ์ ์ฌ๋ฌด๋ถ์๊ฐ์ ๋๋ค. ์๋ ์ ๋ ฅ๊ฐ์ ๋ฐํ์ผ๋ก " | |
| "๊ฐ๊ฐ์๊ฐ๋น, ์๊ฐ์จ, ์์ด์ต์จ, ์์์์ต, ์์ต๋ถ๊ธฐ์ ์ " | |
| "'์ ๋ชฉ: ์์น' ํํ๋ก 5์ค๋ง ์๋ ค์ฃผ์ธ์. ์ถ๊ฐ ์ค๋ช ์์ด ๊ฐ๊ฒฐํ๊ฒ ๋ต๋ณํด์ฃผ์ธ์." | |
| ) | |
| content = f""" | |
| ์ํ๊ท ๋งค์ถ์ก: {avg_monthly_sales} | |
| ์์ฐจ๋ฃ: {rent} | |
| ์ธ๊ฑด๋น: {labor_cost} | |
| ๊ด๋ฆฌ๋น: {management_cost} | |
| ์๋๊ด์ด๋น: {utilities} | |
| ์ง๊ธ์ด์: {current_interest} | |
| ๊ธฐํ๋น์ฉ: {other_cost} | |
| """ | |
| return call_api( | |
| content=content, | |
| system_message=system_message, | |
| max_tokens=500, | |
| temperature=0.7, | |
| top_p=1.0 | |
| ) | |
| ################################### | |
| # Gradio UI | |
| ################################### | |
| def create_app(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ์ฌ๋ฌด ๋ถ์ (7๊ฐ ์ ๋ ฅ) -> ๊ฐ๊ฐ์๊ฐ๋น / ์๊ฐ์จ / ์์ด์ต์จ / ์์์์ต / ์์ต๋ถ๊ธฐ์ ๊ณ์ฐ") | |
| avg_monthly_sales_input = gr.Textbox(label="์ํ๊ท ๋งค์ถ์ก") | |
| rent_input = gr.Textbox(label="์์ฐจ๋ฃ") | |
| labor_cost_input = gr.Textbox(label="์ธ๊ฑด๋น") | |
| management_cost_input = gr.Textbox(label="๊ด๋ฆฌ๋น") | |
| utilities_input = gr.Textbox(label="์๋๊ด์ด๋น") | |
| current_interest_input = gr.Textbox(label="์ง๊ธ์ด์") | |
| other_cost_input = gr.Textbox(label="๊ธฐํ๋น์ฉ") | |
| result_output = gr.Textbox(label="๊ฒฐ๊ณผ (์ ๋ชฉ: ์์น)") | |
| analyze_button = gr.Button("๊ณ์ฐํ๊ธฐ") | |
| analyze_button.click( | |
| fn=analyze_financials, | |
| inputs=[ | |
| avg_monthly_sales_input, | |
| rent_input, | |
| labor_cost_input, | |
| management_cost_input, | |
| utilities_input, | |
| current_interest_input, | |
| other_cost_input | |
| ], | |
| outputs=result_output | |
| ) | |
| demo.launch() | |
| if __name__ == "__main__": | |
| app = create_app() | |
| app.launch() | |