free-ai-chatbot / app.py
tinmanlab's picture
Fix: Simplified app with proper launch config
103b31b verified
"""
무료 AI 챗봇 - HuggingFace Spaces
κ°„λ‹¨ν•˜κ³  μ•ˆμ •μ μΈ 버전
"""
import gradio as gr
from transformers import pipeline
import os
# ν™˜κ²½ 확인
IS_SPACES = os.getenv("SPACE_ID") is not None
print(f"Starting in {'HuggingFace Spaces' if IS_SPACES else 'Local'} mode...")
# λͺ¨λΈ λ‘œλ“œ
print("Loading model...")
generator = pipeline(
'text-generation',
model='microsoft/DialoGPT-small',
device=-1 # CPU
)
print("Model loaded!")
def chat(message, history):
"""챗봇 응닡 생성"""
if not message:
return "λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”."
# νžˆμŠ€ν† λ¦¬κ°€ μ—†μœΌλ©΄ 빈 리슀트둜
if history is None:
history = []
# κ°„λ‹¨ν•œ ν”„λ‘¬ν”„νŠΈ
prompt = f"User: {message}\nBot:"
try:
# ν…μŠ€νŠΈ 생성
response = generator(
prompt,
max_new_tokens=50,
temperature=0.7,
do_sample=True,
pad_token_id=50256
)
# 응닡 μΆ”μΆœ
generated = response[0]['generated_text']
# Bot: 이후 ν…μŠ€νŠΈλ§Œ μΆ”μΆœ
if "Bot:" in generated:
reply = generated.split("Bot:")[-1].strip()
else:
reply = "μ•ˆλ…•ν•˜μ„Έμš”! 무엇을 λ„μ™€λ“œλ¦΄κΉŒμš”?"
return reply
except Exception as e:
print(f"Error: {e}")
return "μ£„μ†‘ν•©λ‹ˆλ‹€. 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€."
# Gradio μΈν„°νŽ˜μ΄μŠ€ (μ΅œλŒ€ν•œ κ°„λ‹¨ν•˜κ²Œ)
demo = gr.ChatInterface(
fn=chat,
title="무료 AI 챗봇",
description="DialoGPT λͺ¨λΈμ„ μ‚¬μš©ν•œ κ°„λ‹¨ν•œ 챗봇",
examples=["μ•ˆλ…•ν•˜μ„Έμš”", "였늘 날씨 μ–΄λ•Œμš”?", "AIκ°€ λ­”κ°€μš”?"],
)
if __name__ == "__main__":
# HuggingFace Spacesμ—μ„œ μ‹€ν–‰μ‹œ μ„€μ •
if IS_SPACES:
demo.launch(
server_name="0.0.0.0",
server_port=7860
)
else:
# λ‘œμ»¬μ—μ„œ μ‹€ν–‰μ‹œ
demo.launch()