Spaces:
No application file
No application file
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 | |
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)) | |