DakuSir commited on
Commit
efeeeb5
·
verified ·
1 Parent(s): 41b38bd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai # For OpenAI integration
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+
5
+ # Set your Nemotron API key
6
+ nemotron_api_key = "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"
7
+ nemotron_api_url = "https://integrate.api.nvidia.com/v1" # Correct API base URL
8
+
9
+ # Initialize the client for Nemotron API
10
+ client = OpenAI(
11
+ base_url=nemotron_api_url,
12
+ api_key=nemotron_api_key
13
+ )
14
+
15
+ # Function to generate descriptions using Nemotron API
16
+ def generate_nemotron_description(product_name, features, audience):
17
+ # Create the prompt based on the input
18
+ input_text = (
19
+ f"Write a highly detailed, professional, and attractive product description for a traditional craft item called '{product_name}'. "
20
+ f"This product has the following features: {features}. "
21
+ f"It is designed for {audience}. Highlight its cultural significance, craftsmanship, uniqueness, and appeal. "
22
+ f"Use emotional and sensory-rich language to make it compelling. The description must be at least 300 words long and suitable for e-commerce or marketing."
23
+ )
24
+
25
+ try:
26
+ # API call to the Nemotron API using the chat completion endpoint
27
+ completion = client.chat.completions.create(
28
+ model="nvidia/nemotron-4-340b-instruct", # Adjust to your model name
29
+ messages=[{"role": "user", "content": input_text}],
30
+ temperature=0.7,
31
+ top_p=0.9,
32
+ max_tokens=250,
33
+ stream=False # Set to False to get the whole response at once
34
+ )
35
+
36
+ # Extract the generated description
37
+ # Correctly access the message content
38
+ generated_text = completion.choices[0].message.content
39
+ return generated_text.strip()
40
+
41
+ except Exception as e:
42
+ return f"An error occurred: {str(e)}"
43
+
44
+ # Gradio interface for ease of use
45
+ interface = gr.Interface(
46
+ fn=generate_nemotron_description,
47
+ inputs=[
48
+ gr.Textbox(label="Product Name", placeholder="e.g., Handwoven Silk Saree"),
49
+ gr.Textbox(label="Features", placeholder="e.g., eco-friendly, handmade, intricate patterns"),
50
+ gr.Textbox(label="Target Audience", placeholder="e.g., luxury buyers, art enthusiasts"),
51
+ ],
52
+ outputs=gr.Textbox(label="Detailed Product Description"),
53
+ title="AI Product Description Generator (Nemotron)",
54
+ description=(
55
+ "Generate long, engaging, and highly detailed product descriptions using Nemotron's API. "
56
+ "Perfect for traditional craft items, e-commerce listings, and marketing purposes."
57
+ ),
58
+ flagging_mode="never"
59
+ )
60
+
61
+ # Launch Gradio app
62
+ interface.launch(share=True)