import streamlit as st import torch from transformers import AutoTokenizer, AutoModelForCausalLM from snap_parse import snap_parse import time # 페이지 설정 st.set_page_config( page_title="DigitSnap", page_icon="⚡", layout="wide" ) # 타이틀 및 소개 st.title("⚡ DigitSnap") st.markdown("**SNAP™ + DeepSeek R1** - AI와의 효율적인 상호작용을 위한 구조화된 명령어 시스템") # 사이드바 - SNAP 가이드 with st.sidebar: st.header("📚 SNAP™ 사용법") st.markdown(""" ### 기본 기호 - `>` : 다음 작업 연결 - `-` : 내용 제외 - `@` : 맥락 지정 - `!` : 강조/필수 ### 사용 예시 - `뉴스 > 요약 -광고` - `수학 설명 @중학생 > 예시 !` - `오늘 뉴스를 한 줄로 요약해줘, 광고는 빼고` """) # 모델 로딩 캐싱 @st.cache_resource def load_model(): try: tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/deepseek-r1:1.5b") model = AutoModelForCausalLM.from_pretrained( "deepseek-ai/deepseek-r1:1.5b", torch_dtype=torch.float16, device_map="auto" ) return tokenizer, model except Exception as e: st.error(f"모델 로딩 실패: {e}") return None, None # 메인 인터페이스 col1, col2 = st.columns([2, 1]) with col1: st.header("💬 명령어 입력") user_input = st.text_area( "SNAP 명령어 또는 자연어를 입력하세요:", placeholder="예: 오늘 뉴스를 한 줄로 요약해줘, 광고는 빼고", height=150 ) if st.button("🚀 실행", type="primary"): if user_input.strip(): # SNAP 파싱 snap_command = snap_parse(user_input) # 파싱 결과 표시 with col2: st.header("🔧 SNAP 파싱 결과") st.code(snap_command, language="text") # 모델 실행 with st.spinner("DeepSeek R1이 응답을 생성하고 있습니다..."): tokenizer, model = load_model() if tokenizer and model: try: # 토큰화 및 생성 inputs = tokenizer(snap_command, return_tensors="pt") with torch.no_grad(): outputs = model.generate( **inputs, max_new_tokens=256, temperature=0.7, do_sample=True, pad_token_id=tokenizer.eos_token_id ) # 결과 디코딩 result = tokenizer.decode(outputs[0], skip_special_tokens=True) # 입력 부분 제거 if snap_command in result: result = result.replace(snap_command, "").strip() st.header("🤖 DeepSeek R1 응답") st.markdown(result) except Exception as e: st.error(f"생성 중 오류 발생: {e}") else: st.error("모델을 로딩할 수 없습니다.") else: st.warning("명령어를 입력해주세요.") with col2: if not user_input.strip(): st.header("🔧 SNAP 파싱 결과") st.info("명령어를 입력하면 파싱 결과가 여기에 표시됩니다.") # 하단 정보 st.markdown("---") st.markdown(""" ### 📋 DigitSnap 정보 - **SNAP™**: Simple Natural AI Protocol - AI 상호작용을 위한 구조화된 명령어 언어 - **DeepSeek R1**: 고성능 오픈소스 언어모델 - **부분 자연어 지원**: 자연어와 SNAP 기호를 자유롭게 혼용 가능 - **토큰 효율성**: 최대 70% 토큰 절약으로 빠른 응답 """)