cur / app.py
KIM2683's picture
Update app.py
c80dbfa verified
import fitz # PyMuPDF
import gradio as gr
import os
career_to_courses = {
"의학전문사서": {
"전공": ["간호학과", "문헌정보학과"],
"추천과목": ["해부학", "정보분류론", "문헌정보학개론", "임상실습"]
},
"데이터 사이언티스트": {
"전공": ["컴퓨터공학과", "빅데이터응용학과"],
"추천과목": ["빅데이터의이해", "phython기초", "빅데이터분석"]
},
"인공지능 연구원": {
"전공": ["컴퓨터공학과", "전자공학과"],
"추천과목": ["인공지능기초", "딥러닝", "컴퓨터비전"]
},
"소프트웨어 개발자": {
"전공": ["컴퓨터공학과", "기계공학과"],
"추천과목": ["자료구조", "운영체제", "웹프로그래밍"]
},
"경영 컨설턴트": {
"전공": ["경영학과", "경제학과"],
"추천과목": ["회계원리", "마케팅", "경영전략"]
},
"환경공학자": {
"전공": ["환경공학과", "화학공학과"],
"추천과목": ["환경오염학", "수질관리", "대기환경학"]
},
"건축전문사서": {
"전공": ["건축학과", "문헌정보학과"],
"추천과목": ["건축설계", "문헌정보학개론", "재료학"]
},
"심리상담사": {
"전공": ["심리학과", "사회복지학과"],
"추천과목": ["상담심리학", "발달심리학", "사회복지개론"]
},
"지식재산 전문가": {
"전공": ["법학과", "경제학과"],
"추천과목": ["지식재산법", "상표법", "특허정보분석"]
},
"아동 콘텐츠 기획자": {
"전공": ["아동복지학과", "미디어학과"],
"추천과목": ["아동발달", "콘텐츠기획", "미디어제작"]
}
}
career_to_pdf_pages = {
"의학전문사서": [354, 57],
"데이터 사이언티스트": [378, 397],
"인공지능 연구원": [378, 111],
"소프트웨어 개발자": [378, 142],
"경영 컨설턴트": [173, 202],
"환경공학자": [334, 335, 155],
"건축전문사서": [57, 124],
"심리상담사": [258, 268],
"지식재산 전문가": [225, 202],
"아동 콘텐츠 기획자": [266, 307]
}
PDF_PATH = "교육과정_편성표.pdf" # PDF 파일 경로
def recommend_courses_with_pdf(user_input):
user_input_lower = user_input.lower()
for career, info in career_to_courses.items():
if career.lower() in user_input_lower:
majors = ", ".join(info["전공"])
courses = ", ".join(info["추천과목"])
text = f"'{career}'가 되고 싶다면 추천 전공은 [{majors}]이고, 수강하면 좋은 과목은 [{courses}]입니다."
pages = career_to_pdf_pages.get(career, [])
images = []
if pages and os.path.exists(PDF_PATH):
doc = fitz.open(PDF_PATH)
for p in pages:
# 페이지 번호는 1부터 시작, fitz는 0부터 시작이니까 -1 해줌
page = doc.load_page(p - 1)
pix = page.get_pixmap(dpi=150) # 해상도 조정 가능
img_path = f"/tmp/{career}_page{p}.png"
pix.save(img_path)
images.append(img_path)
doc.close()
return text, images if images else None
return "해당 진로에 대한 정보가 없습니다. 다른 진로명을 입력해 주세요.", None
app_code = f'''
import fitz
import gradio as gr
import os
PDF_PATH = "{PDF_PATH}"
career_to_courses = {career_to_courses}
career_to_pdf_pages = {career_to_pdf_pages}
def recommend_courses_with_pdf(user_input):
user_input_lower = user_input.lower()
for career, info in career_to_courses.items():
if career.lower() in user_input_lower:
majors = ", ".join(info["전공"])
courses = ", ".join(info["추천과목"])
text = f"'{{career}}'가 되고 싶다면 추천 전공은 [{{majors}}]이고, 수강하면 좋은 과목은 [{{courses}}]입니다."
pages = career_to_pdf_pages.get(career, [])
images = []
if pages and os.path.exists(PDF_PATH):
doc = fitz.open(PDF_PATH)
for p in pages:
page = doc.load_page(p - 1)
pix = page.get_pixmap(dpi=150)
img_path = f"/tmp/{{career}}_page{{p}}.png"
pix.save(img_path)
images.append(img_path)
doc.close()
return text, images if images else None
return "해당 진로에 대한 정보가 없습니다. 다른 진로명을 입력해 주세요.", None
iface = gr.Interface(
fn=recommend_courses_with_pdf,
inputs=gr.Textbox(lines=2, placeholder="희망 진로를 입력하세요 (예: 의학전문사서)"),
outputs=[gr.Textbox(label="추천 전공 및 과목"), gr.Gallery(label="관련 PDF 페이지 이미지")],
title="진로별 전공·과목 추천 및 PDF 연계 챗봇",
description="진로명을 입력하면 관련 전공, 과목과 함께 PDF 일부 페이지를 이미지로 보여줍니다."
)
if __name__ == "__main__":
iface.launch()
'''
with open('/content/hf_deployment/app.py', 'w', encoding='utf-8') as f:
f.write(app_code)