runse / main.py
showme's picture
Update main.py
71d86ad verified
raw
history blame
3.39 kB
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import T5ForConditionalGeneration, T5Tokenizer
# 创建FastAPI实例
app = FastAPI()
# 加载T5模型和Tokenizer
model_name = "google-t5/t5-small"
model = T5ForConditionalGeneration.from_pretrained(model_name)
tokenizer = T5Tokenizer.from_pretrained(model_name)
# 定义输入数据的结构
class ContentRequest(BaseModel):
content: str
# Humanize的Prompt
def generate_humanized_content(content: str) -> str:
prompt = f"""
You are a skilled writer tasked with rewriting AI-generated text to make it sound more human, engaging, and relatable. Please follow these instructions:
**Humanizing Instructions:**
To generate human-written text or convert AI-written text into humanized content, here are some instructions you can give to a Large Language Model (LLM):
1. Write in a conversational tone: Imagine you're having a discussion with a friend. Use everyday language and avoid jargon or overly formal tone.
2. Add personal touches: Include personal anecdotes, emotions, or experiences to make the text more relatable and authentic.
3. Use contractions and colloquialisms: Incorporate contractions (e.g., "don't" instead of "do not") and colloquial expressions (e.g., "break a leg") to give the text a more human feel.
4. Vary sentence structure: Mix up short and long sentences to create a natural flow, just like human writing.
5. Show, don't tell: Instead of stating facts, use descriptive language to paint a picture in the reader's mind.
**For converting AI-written text into humanized content**:
1. Rewrite in a more conversational tone: Take the AI-written text and rephrase it in a more relaxed, everyday language.
2. Add emotional depth: Inject emotions, empathy, or personal experiences to make the text more relatable and engaging.
3. Use more descriptive language: Replace generic terms with vivid, descriptive words to add flavor and personality to the text.
4. Break up long sentences: Split lengthy sentences into shorter, more manageable ones to improve readability.
5. Add idioms and colloquialisms: Incorporate common idioms and colloquial expressions to give the text a more human-like quality.
6. Read it aloud: Read the text aloud to ensure it sounds natural and conversational.
**Additional Tips**:
- Use active voice: It's more engaging and easier to read than passive voice.
- Show vulnerability: Share imperfections, doubts, or fears to make the text more relatable and authentic.
- Use humor: Humor can help humanize the text and make it more enjoyable to read.
- Edit and refine: Review the text multiple times to refine the language, tone, and flow.
Now, please humanize the following text:
{content}
"""
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
output = model.generate(inputs["input_ids"], max_length=512, num_beams=4, early_stopping=True)
return tokenizer.decode(output[0], skip_special_tokens=True)
# API端点,接收内容并返回“人性化”后的文本
@app.post("/humanize/")
async def humanize_content(request: ContentRequest):
humanized_content = generate_humanized_content(request.content)
return {"humanized_content": humanized_content}
# 启动FastAPI应用时,使用`uvicorn main:app --reload`