Spaces:
No application file
No application file
File size: 2,570 Bytes
5e15116 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from huggingface_hub import InferenceClient
import os
from dotenv import load_dotenv
load_dotenv()
hf_token = os.getenv("huggingface_api_token")
app = FastAPI()
client = InferenceClient(model="meta-llama/Llama-3.1-70B-Instruct", api_key=hf_token)
# Define request and response models for validation
class ProductDetails(BaseModel):
brand_name:str
product_name:str
product_category:str
product_features:str
tone: str='Professional'
max_tokens: int = 500
class GeneratedDescription(BaseModel):
description: str
def system_prompt_content(product_name, product_category, product_features, tone, brand_name):
prompt = f"""
You are an expert e-commerce product description writer. Generate a {tone} product description using the following structured template:
1. **Introduction**: Briefly describe the {product_name}, highlighting its main purpose and key selling points. The brand name is {brand_name}
2. **Key Features**: Provide a list of key features for the {product_name}. Explain each feature clearly, emphasizing how it benefits the user. The features are: {product_features}.
3. **Benefits**: Expand on why these features matter. Describe how they solve problems or enhance the customer experience. Focus on customer value.
4. **Call to Action**: End the description with a strong call to action, encouraging the customer to make a purchase or learn more.
Tailor the description based on the category of the product: {product_category}.
Keep the tone {tone} as specified. Ensure the description is engaging, informative, and follows a clear structure.
NOTE: Do not say anything other than description
"""
return prompt
# FastAPI endtpoint to generate product description
@app.post("/generate-description")
async def generate_description(details: ProductDetails):
try:
complete_prompt = system_prompt_content(
details.brand_name,
details.product_category,
details.product_features,
details.tone,
details.brand_name
)
response=client.text_generation(complete_prompt, max_new_tokens=details.max_tokens)
if response and isinstance(response,str):
return {"description": response}
else:
raise HTTPException(status_code=500, detail="invalid response")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
|