Spaces:
Running
Running
Update UI to support models that are not from OpenAI, based on LiteLLM.
Browse files- app.py +36 -17
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,13 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import requests
|
4 |
-
from
|
|
|
|
|
|
|
5 |
from crewai_tools import ScrapeWebsiteTool
|
6 |
from crewai.tools import BaseTool
|
7 |
-
from langchain_openai import ChatOpenAI
|
8 |
-
|
9 |
-
|
10 |
-
# --- CrewAI Integration ---
|
11 |
|
12 |
class ShortenerTool(BaseTool):
|
13 |
name: str = "URL Shortener Tool"
|
@@ -46,13 +45,25 @@ class CalculateDiscountedPriceTool(BaseTool):
|
|
46 |
return round(discounted_price, 2)
|
47 |
|
48 |
class SocialMediaCrew:
|
49 |
-
def __init__(self, openai_api_key: str, natura_api_token: str):
|
50 |
self.openai_api_key = openai_api_key
|
51 |
self.natura_api_token = natura_api_token
|
|
|
|
|
52 |
self.scrape_tool = ScrapeWebsiteTool()
|
53 |
self.shortener_tool = ShortenerTool(natura_api_token=self.natura_api_token)
|
54 |
self.calculate_discounted_price_tool = CalculateDiscountedPriceTool()
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
self.product_analyst = Agent(
|
57 |
role='Product Analyst',
|
58 |
goal='Analyze the provided URL and extract key product information',
|
@@ -60,7 +71,7 @@ class SocialMediaCrew:
|
|
60 |
verbose=True,
|
61 |
tools=[self.scrape_tool, self.shortener_tool, self.calculate_discounted_price_tool],
|
62 |
allow_delegation=False,
|
63 |
-
llm=
|
64 |
)
|
65 |
|
66 |
self.social_media_copywriter = Agent(
|
@@ -69,7 +80,7 @@ class SocialMediaCrew:
|
|
69 |
backstory=("You are a creative copywriter specialized in the beauty and fragrance market. You know how to craft posts that are engaging, persuasive, and tailored for a Portuguese-speaking audience. You are an expert in using emojis and hashtags to increase engagement."),
|
70 |
verbose=True,
|
71 |
allow_delegation=False,
|
72 |
-
llm=
|
73 |
)
|
74 |
|
75 |
def run_crew(self, product_url: str, main_cupom: str, main_cupom_discount_percentage: float, cupom_1: str, cupom_2: str) -> str:
|
@@ -111,7 +122,7 @@ class SocialMediaCrew:
|
|
111 |
De ~~{{ORIGINAL PRICE}}~~
|
112 |
🔥Por {{CUPOM DISCOUNTED PRICE}} 🔥
|
113 |
|
114 |
-
|
115 |
|
116 |
🛒 Link >>> {{short_url}}
|
117 |
|
@@ -129,20 +140,25 @@ Ensure a URL is always present in the output. Include a clear call to action and
|
|
129 |
crew = Crew(
|
130 |
agents=[self.product_analyst, self.social_media_copywriter],
|
131 |
tasks=[analyze_product_task, create_post_task],
|
132 |
-
process=Process.sequential
|
133 |
-
openai_api_key=self.openai_api_key
|
134 |
)
|
135 |
|
136 |
print(f"Crew is kicking off for URL: {product_url}")
|
137 |
result = crew.kickoff()
|
138 |
return result
|
139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
# --- Gradio Interface ---
|
141 |
-
def generate_ad(product_url: str, main_cupom: str, main_cupom_discount_percentage: float, cupom_1: str, cupom_2: str, openai_api_key: str, natura_api_token: str):
|
142 |
-
if not openai_api_key or not natura_api_token:
|
143 |
return "Please configure your API keys in the settings section below."
|
144 |
|
145 |
-
social_media_crew = SocialMediaCrew(openai_api_key, natura_api_token)
|
146 |
result = social_media_crew.run_crew(product_url, main_cupom, main_cupom_discount_percentage, cupom_1, cupom_2)
|
147 |
|
148 |
if result == "INVALID_URL":
|
@@ -169,12 +185,15 @@ with gr.Blocks() as demo:
|
|
169 |
with gr.Tab("Settings"):
|
170 |
gr.Markdown("### ⚙️ API Key Settings")
|
171 |
gr.Markdown("Enter your API keys below. These will be used for the current session.")
|
172 |
-
openai_key_input = gr.Textbox(label="OPENAI_API_KEY", type="password")
|
173 |
-
natura_token_input = gr.Textbox(label="NATURA_API_TOKEN", type="password")
|
|
|
|
|
174 |
|
|
|
175 |
# No save button needed as keys are passed directly
|
176 |
gr.Markdown("API keys are used directly from these fields when you click 'Generate Ad'. They are not saved persistently.")
|
177 |
|
178 |
-
generate_button.click(generate_ad, inputs=[url_input, main_cupom_input, main_cupom_discount_percentage_input, cupom_1_input, cupom_2_input, openai_key_input, natura_token_input], outputs=ad_output)
|
179 |
|
180 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import requests
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
from crewai import Agent, Task, Crew, Process, LLM
|
8 |
from crewai_tools import ScrapeWebsiteTool
|
9 |
from crewai.tools import BaseTool
|
|
|
|
|
|
|
|
|
10 |
|
11 |
class ShortenerTool(BaseTool):
|
12 |
name: str = "URL Shortener Tool"
|
|
|
45 |
return round(discounted_price, 2)
|
46 |
|
47 |
class SocialMediaCrew:
|
48 |
+
def __init__(self, openai_api_key: str, natura_api_token: str, openai_base_url: str, openai_model_name: str):
|
49 |
self.openai_api_key = openai_api_key
|
50 |
self.natura_api_token = natura_api_token
|
51 |
+
self.openai_base_url = openai_base_url
|
52 |
+
self.openai_model_name = openai_model_name
|
53 |
self.scrape_tool = ScrapeWebsiteTool()
|
54 |
self.shortener_tool = ShortenerTool(natura_api_token=self.natura_api_token)
|
55 |
self.calculate_discounted_price_tool = CalculateDiscountedPriceTool()
|
56 |
|
57 |
+
print("Initializing SocialMediaCrew with BASE URL:", self.openai_base_url)
|
58 |
+
print("Using OpenAI Model:", self.openai_model_name)
|
59 |
+
print("Using OpenAI Key:", self.openai_api_key[:10])
|
60 |
+
|
61 |
+
llm = LLM(
|
62 |
+
api_key=self.openai_api_key,
|
63 |
+
model=self.openai_model_name,
|
64 |
+
base_url=self.openai_base_url
|
65 |
+
)
|
66 |
+
|
67 |
self.product_analyst = Agent(
|
68 |
role='Product Analyst',
|
69 |
goal='Analyze the provided URL and extract key product information',
|
|
|
71 |
verbose=True,
|
72 |
tools=[self.scrape_tool, self.shortener_tool, self.calculate_discounted_price_tool],
|
73 |
allow_delegation=False,
|
74 |
+
llm=llm
|
75 |
)
|
76 |
|
77 |
self.social_media_copywriter = Agent(
|
|
|
80 |
backstory=("You are a creative copywriter specialized in the beauty and fragrance market. You know how to craft posts that are engaging, persuasive, and tailored for a Portuguese-speaking audience. You are an expert in using emojis and hashtags to increase engagement."),
|
81 |
verbose=True,
|
82 |
allow_delegation=False,
|
83 |
+
llm=llm
|
84 |
)
|
85 |
|
86 |
def run_crew(self, product_url: str, main_cupom: str, main_cupom_discount_percentage: float, cupom_1: str, cupom_2: str) -> str:
|
|
|
122 |
De ~~{{ORIGINAL PRICE}}~~
|
123 |
🔥Por {{CUPOM DISCOUNTED PRICE}} 🔥
|
124 |
|
125 |
+
🎟️ USE O CUPOM >>> {main_cupom}
|
126 |
|
127 |
🛒 Link >>> {{short_url}}
|
128 |
|
|
|
140 |
crew = Crew(
|
141 |
agents=[self.product_analyst, self.social_media_copywriter],
|
142 |
tasks=[analyze_product_task, create_post_task],
|
143 |
+
process=Process.sequential
|
|
|
144 |
)
|
145 |
|
146 |
print(f"Crew is kicking off for URL: {product_url}")
|
147 |
result = crew.kickoff()
|
148 |
return result
|
149 |
|
150 |
+
def clean_env_vars():
|
151 |
+
os.environ.pop("OPENAI_API_KEY", None)
|
152 |
+
os.environ.pop("NATURA_API_TOKEN", None)
|
153 |
+
os.environ.pop("OPENAI_BASE_URL", None)
|
154 |
+
os.environ.pop("OPENAI_MODEL_NAME", None)
|
155 |
+
|
156 |
# --- Gradio Interface ---
|
157 |
+
def generate_ad(product_url: str, main_cupom: str, main_cupom_discount_percentage: float, cupom_1: str, cupom_2: str, openai_api_key: str, natura_api_token: str, openai_base_url: str, openai_model_name: str):
|
158 |
+
if not openai_api_key or not natura_api_token or not openai_model_name or not openai_base_url:
|
159 |
return "Please configure your API keys in the settings section below."
|
160 |
|
161 |
+
social_media_crew = SocialMediaCrew(openai_api_key, natura_api_token, openai_base_url, openai_model_name)
|
162 |
result = social_media_crew.run_crew(product_url, main_cupom, main_cupom_discount_percentage, cupom_1, cupom_2)
|
163 |
|
164 |
if result == "INVALID_URL":
|
|
|
185 |
with gr.Tab("Settings"):
|
186 |
gr.Markdown("### ⚙️ API Key Settings")
|
187 |
gr.Markdown("Enter your API keys below. These will be used for the current session.")
|
188 |
+
openai_key_input = gr.Textbox(label="OPENAI_API_KEY", type="password", value=os.getenv("OPENAI_API_KEY", ""))
|
189 |
+
natura_token_input = gr.Textbox(label="NATURA_API_TOKEN", type="password", value=os.getenv("NATURA_API_TOKEN", ""))
|
190 |
+
openai_base_url_input = gr.Textbox(label="OPENAI_BASE_URL", value=os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1"))
|
191 |
+
openai_model_name_input = gr.Textbox(label="OPENAI_MODEL_NAME", value=os.getenv("OPENAI_MODEL_NAME", "gpt-4o-mini"))
|
192 |
|
193 |
+
clean_env_vars()
|
194 |
# No save button needed as keys are passed directly
|
195 |
gr.Markdown("API keys are used directly from these fields when you click 'Generate Ad'. They are not saved persistently.")
|
196 |
|
197 |
+
generate_button.click(generate_ad, inputs=[url_input, main_cupom_input, main_cupom_discount_percentage_input, cupom_1_input, cupom_2_input, openai_key_input, natura_token_input, openai_base_url_input, openai_model_name_input], outputs=ad_output)
|
198 |
|
199 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
crewai>=0.148.0
|
2 |
crewai-tools>=0.55.0
|
3 |
-
gradio>=5.38.0
|
|
|
|
1 |
crewai>=0.148.0
|
2 |
crewai-tools>=0.55.0
|
3 |
+
gradio>=5.38.0
|
4 |
+
litellm
|