HOJOON23's picture
Update app.py
47e2f84 verified
import gradio as gr
from transformers import pipeline
# λͺ¨λΈ λ‘œλ“œ
try:
generator = pipeline("text-generation", model="bigscience/bloomz-560m") # ν•„μš”μ— 따라 λͺ¨λΈ λ³€κ²½
except Exception as e:
generator = None
error_message = f"λͺ¨λΈ λ‘œλ“œ 였λ₯˜: {str(e)}"
# 응닡 생성 ν•¨μˆ˜
def generate_reply(review):
if not generator:
return f"λͺ¨λΈμ„ λ‘œλ“œν•  수 μ—†μŠ΅λ‹ˆλ‹€. 였λ₯˜: {error_message}"
# ν”„λ‘¬ν”„νŠΈ 생성
prompt = f"""
λ‹€μŒμ€ 고객 λ¦¬λ·°μž…λ‹ˆλ‹€:
리뷰: "{review}"
이 리뷰에 λŒ€ν•΄ μ •μ€‘ν•˜κ³  κ°μ‚¬μ˜ λœ»μ„ 담은 고객 μ„œλΉ„μŠ€ νŒ€μ˜ 닡변을 μž‘μ„±ν•˜μ„Έμš”.
λ‹΅λ³€ 예: "κ³ κ°λ‹˜μ˜ μ†Œμ€‘ν•œ 의견 κ°μ‚¬ν•©λ‹ˆλ‹€. μ•žμœΌλ‘œλ„ 더 λ‚˜μ€ μ„œλΉ„μŠ€λ₯Ό μ œκ³΅ν•˜κΈ° μœ„ν•΄ λ…Έλ ₯ν•˜κ² μŠ΅λ‹ˆλ‹€."
"""
try:
# λͺ¨λΈ 호좜
result = generator(prompt, max_new_tokens=50, do_sample=True, temperature=0.7)
# 디버깅 정보 좜λ ₯
debug_info = f"μž…λ ₯된 ν”„λ‘¬ν”„νŠΈ: {prompt}\nλͺ¨λΈ 응닡: {result}"
if result and "generated_text" in result[0]:
generated_text = result[0]["generated_text"]
# ν”„λ‘¬ν”„νŠΈ λ‚΄μš© 제거
if prompt in generated_text:
generated_text = generated_text.replace(prompt, "").strip()
return f"μƒμ„±λœ λ‹΅λ³€: {generated_text}\n\n[디버깅 정보]\n{debug_info}"
else:
return f"응닡 처리 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.\n\n[디버깅 정보]\n{debug_info}"
except Exception as e:
return f"API 호좜 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
iface = gr.Interface(
fn=generate_reply,
inputs="text",
outputs="text",
title="Review Reply Generator",
description="고객 리뷰λ₯Ό μž…λ ₯ν•˜λ©΄ μ •μ€‘ν•œ 닡변을 μƒμ„±ν•©λ‹ˆλ‹€."
)
# Space μ‹€ν–‰
iface.launch()