Vinifinn6's picture
Adicionar limitação de conteúdo e tratamento de erros
1a48819 verified
import gradio as gr
from transformers import pipeline
# Initialize the pipeline
pipe = pipeline("text-generation", model="mlabonne/Qwen3-8B-abliterated")
def chat(text):
try:
# Limit input text to prevent excessive processing
if len(text) > 1000:
text = text[:1000]
# Generate response with limited tokens
res = pipe(text, max_new_tokens=50)
generated_text = res[0]['generated_text']
# Limit response length to control Content-Length
max_response_length = 2000
if len(generated_text) > max_response_length:
generated_text = generated_text[:max_response_length] + "..."
return generated_text
except Exception as e:
# Return user-friendly error message instead of critical failure
return f"Desculpe, ocorreu um erro ao processar sua solicitação. Tente novamente com um texto mais simples."
demo = gr.Interface(
fn=chat,
inputs="text",
outputs="text",
title="Qwen3-8B Abliterated Chatbot"
)
demo.launch()