Spaces:
Sleeping
Sleeping
migrate to open ai
Browse files- app_openai.py +85 -0
app_openai.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# Initialize OpenAI client
|
| 6 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
+
|
| 8 |
+
# Streaming response function
|
| 9 |
+
def respond(
|
| 10 |
+
message,
|
| 11 |
+
history: list[tuple[str, str]],
|
| 12 |
+
system_message,
|
| 13 |
+
segment_profile,
|
| 14 |
+
value_proposition,
|
| 15 |
+
promotion,
|
| 16 |
+
subject,
|
| 17 |
+
ask_for_subject_suggestions,
|
| 18 |
+
max_tokens,
|
| 19 |
+
temperature,
|
| 20 |
+
top_p,
|
| 21 |
+
):
|
| 22 |
+
# Compose enhanced system message
|
| 23 |
+
enhanced_system_message = (
|
| 24 |
+
f"{system_message}\n\n"
|
| 25 |
+
f"Segment Profile: {segment_profile}\n"
|
| 26 |
+
f"Value Proposition: {value_proposition}\n"
|
| 27 |
+
f"Goal and Promotion: {promotion}\n"
|
| 28 |
+
f"Subject Line: {subject}\n"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if ask_for_subject_suggestions:
|
| 32 |
+
enhanced_system_message += " The user is also asking for subject line suggestions to catch their customer's attention and improve Email Open Rate."
|
| 33 |
+
|
| 34 |
+
# Build message history
|
| 35 |
+
messages = [{"role": "system", "content": enhanced_system_message}]
|
| 36 |
+
for user_msg, assistant_msg in history:
|
| 37 |
+
if user_msg:
|
| 38 |
+
messages.append({"role": "user", "content": user_msg})
|
| 39 |
+
if assistant_msg:
|
| 40 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 41 |
+
messages.append({"role": "user", "content": message})
|
| 42 |
+
|
| 43 |
+
# Stream response from OpenAI
|
| 44 |
+
response_text = ""
|
| 45 |
+
try:
|
| 46 |
+
response = client.chat.completions.create(
|
| 47 |
+
model="gpt-4o", # or "gpt-4o-mini" when available
|
| 48 |
+
messages=messages,
|
| 49 |
+
max_tokens=max_tokens,
|
| 50 |
+
temperature=temperature,
|
| 51 |
+
top_p=top_p,
|
| 52 |
+
stream=True,
|
| 53 |
+
)
|
| 54 |
+
for chunk in response:
|
| 55 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 56 |
+
token = chunk.choices[0].delta.content
|
| 57 |
+
response_text += token
|
| 58 |
+
yield response_text
|
| 59 |
+
except Exception as e:
|
| 60 |
+
yield f"❌ An error occurred: {str(e)}"
|
| 61 |
+
|
| 62 |
+
# Gradio interface
|
| 63 |
+
demo = gr.ChatInterface(
|
| 64 |
+
fn=respond,
|
| 65 |
+
additional_inputs=[
|
| 66 |
+
gr.Textbox(
|
| 67 |
+
value="You are a friendly Chatbot, a digital marketing expert and a talented copywriter. You are trying to help a user write a creative email that can achieve campaign goals, a high Open Rate, CTR and conversion rate - based on user input.",
|
| 68 |
+
label="Instructions to Bot",
|
| 69 |
+
),
|
| 70 |
+
gr.Textbox(label="Your Target Customer Segment Profile", placeholder="Describe the profile of your target customer segment (e.g., age, gender, interests, profession)"),
|
| 71 |
+
gr.Textbox(label="Your Value Proposition", placeholder="Describe how your solution to customer problems offers them unique value"),
|
| 72 |
+
gr.Textbox(label="Campaign Goal, Special Event, Promotion and Call to Action", placeholder="Describe your campaign goal, a special event, promotion and Call to Action that you hope your target segment will act upon"),
|
| 73 |
+
gr.Textbox(label="Subject Line", placeholder="Enter the Subject Line of the Email or ask for suggestions"),
|
| 74 |
+
gr.Checkbox(label="Ask for Subject Line Suggestions", value=False),
|
| 75 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 76 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 77 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 78 |
+
],
|
| 79 |
+
title="Email Copywriter",
|
| 80 |
+
description="This app creates a customized email that resonates with your customers to improve CTR and conversion. Based on your input. Powered by OpenAI GPT-4o. Developed by wn. Disclaimer: AI makes mistakes. Use with caution and at your own risk!",
|
| 81 |
+
type="messages",
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo.launch()
|