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