Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from openai import OpenAI | |
| # Initialize OpenAI client | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| # Streaming response function | |
| def respond( | |
| message, | |
| history: list[tuple[str, str]], | |
| system_message, | |
| segment_profile, | |
| value_proposition, | |
| promotion, | |
| subject, | |
| ask_for_subject_suggestions, | |
| max_tokens, | |
| temperature, | |
| top_p, | |
| ): | |
| # Compose enhanced system message | |
| enhanced_system_message = ( | |
| f"{system_message}\n\n" | |
| f"Segment Profile: {segment_profile}\n" | |
| f"Value Proposition: {value_proposition}\n" | |
| f"Goal and Promotion: {promotion}\n" | |
| f"Subject Line: {subject}\n" | |
| ) | |
| if ask_for_subject_suggestions: | |
| enhanced_system_message += " The user is also asking for subject line suggestions to catch their customer's attention and improve Email Open Rate." | |
| # Build message history | |
| messages = [{"role": "system", "content": enhanced_system_message}] | |
| for user_msg, assistant_msg in history: | |
| if user_msg: | |
| messages.append({"role": "user", "content": user_msg}) | |
| if assistant_msg: | |
| messages.append({"role": "assistant", "content": assistant_msg}) | |
| messages.append({"role": "user", "content": message}) | |
| # Stream response from OpenAI | |
| response_text = "" | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-4o-mini", # or "gpt-4o" if mini unavailable | |
| messages=messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| stream=True, | |
| ) | |
| for chunk in response: | |
| if chunk.choices and chunk.choices[0].delta.content: | |
| token = chunk.choices[0].delta.content | |
| response_text += token | |
| yield response_text | |
| except Exception as e: | |
| yield f"❌ An error occurred: {str(e)}" | |
| # Gradio interface | |
| demo = gr.ChatInterface( | |
| fn=respond, | |
| additional_inputs=[ | |
| gr.Textbox( | |
| 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.", | |
| label="Instructions to Bot", | |
| ), | |
| gr.Textbox(label="Your Target Customer Segment Profile", placeholder="Describe the profile of your target customer segment (e.g., age, gender, interests, profession)"), | |
| gr.Textbox(label="Your Value Proposition", placeholder="Describe how your solution to customer problems offers them unique value"), | |
| 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"), | |
| gr.Textbox(label="Subject Line", placeholder="Enter the Subject Line of the Email or ask for suggestions"), | |
| gr.Checkbox(label="Ask for Subject Line Suggestions", value=False), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"), | |
| ], | |
| title="Email Copywriter", | |
| 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!", | |
| type="messages", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |