Spaces:
Sleeping
Sleeping
migrate to Open Ai
Browse files- app_openAi.py +94 -0
app_openAi.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# Initialize the OpenAI client
|
| 6 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 7 |
+
|
| 8 |
+
# Response generation with streaming
|
| 9 |
+
def respond(
|
| 10 |
+
message,
|
| 11 |
+
history: list[dict],
|
| 12 |
+
system_message,
|
| 13 |
+
customer_profile,
|
| 14 |
+
customer_goals,
|
| 15 |
+
company_solution,
|
| 16 |
+
main_topic,
|
| 17 |
+
ask_for_topic_suggestions,
|
| 18 |
+
max_tokens,
|
| 19 |
+
temperature,
|
| 20 |
+
top_p,
|
| 21 |
+
):
|
| 22 |
+
# Compose system message
|
| 23 |
+
enhanced_system_message = (
|
| 24 |
+
f"{system_message}\n\n"
|
| 25 |
+
f"Customer Profile: {customer_profile}\n"
|
| 26 |
+
f"Customer Goals, Pain Points, Obstacles, Wishes and Preferences: {customer_goals}\n"
|
| 27 |
+
f"Company solutions, products or services, value proposition and differentiators to solve customer problem: {company_solution}\n"
|
| 28 |
+
f"Main Topic: {main_topic}\n"
|
| 29 |
+
)
|
| 30 |
+
if ask_for_topic_suggestions:
|
| 31 |
+
enhanced_system_message += "\nThe user is also asking for topic suggestions to address their customer's needs."
|
| 32 |
+
|
| 33 |
+
# Format messages
|
| 34 |
+
messages = [{"role": "system", "content": enhanced_system_message}]
|
| 35 |
+
for m in history:
|
| 36 |
+
messages.append({"role": "user", "content": m["user"]})
|
| 37 |
+
if m.get("assistant"):
|
| 38 |
+
messages.append({"role": "assistant", "content": m["assistant"]})
|
| 39 |
+
messages.append({"role": "user", "content": message})
|
| 40 |
+
|
| 41 |
+
# Stream OpenAI response
|
| 42 |
+
response_text = ""
|
| 43 |
+
try:
|
| 44 |
+
response = client.chat.completions.create(
|
| 45 |
+
model="gpt-4o-mini", # or other model as available
|
| 46 |
+
messages=messages,
|
| 47 |
+
max_tokens=max_tokens,
|
| 48 |
+
temperature=temperature,
|
| 49 |
+
top_p=top_p,
|
| 50 |
+
stream=True,
|
| 51 |
+
)
|
| 52 |
+
for chunk in response:
|
| 53 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 54 |
+
token = chunk.choices[0].delta.content
|
| 55 |
+
response_text += token
|
| 56 |
+
yield response_text
|
| 57 |
+
except Exception as e:
|
| 58 |
+
yield f"❌ An error occurred: {str(e)}"
|
| 59 |
+
|
| 60 |
+
# Gradio interface definition
|
| 61 |
+
demo = gr.ChatInterface(
|
| 62 |
+
fn=respond,
|
| 63 |
+
additional_inputs=[
|
| 64 |
+
gr.Textbox(
|
| 65 |
+
value="You are a friendly Chatbot, a digital marketing expert and a talented copywriter. You are trying to help a user write a creative post to improve their SEO based on their input.",
|
| 66 |
+
label="Instructions to Bot",
|
| 67 |
+
),
|
| 68 |
+
gr.Textbox(label="Your Customer Profile", placeholder="Describe your customer profile (e.g., age, interests, profession)"),
|
| 69 |
+
gr.Textbox(
|
| 70 |
+
label="Customer Goals, Pain Points, Obstacles, Wishes and Preferences",
|
| 71 |
+
placeholder="Describe your customer's goals, pain points, concerns, obstacles, wishes, and preferences",
|
| 72 |
+
),
|
| 73 |
+
gr.Textbox(
|
| 74 |
+
label="Company solutions, products or services",
|
| 75 |
+
placeholder="Describe your company's solutions, products or services, value proposition and differentiators",
|
| 76 |
+
),
|
| 77 |
+
gr.Textbox(label="Main Topic", placeholder="Enter the main topic of the post"),
|
| 78 |
+
gr.Checkbox(label="Ask for Topic Suggestions", value=False),
|
| 79 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 80 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 81 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 82 |
+
],
|
| 83 |
+
title="SEO Assistant",
|
| 84 |
+
description=(
|
| 85 |
+
"This app provides customized content that resonates with your customers to improve your SEO. "
|
| 86 |
+
"Based on your input. Powered by OpenAI GPT-4o-mini and domain expertise. "
|
| 87 |
+
"Expand Additional Inputs by clicking on the arrow, input more details about your customers, then a message describing what you need the assistant to do for you. "
|
| 88 |
+
"Developed by wn. Disclaimer: AI can make mistakes. Use with caution and at your own risk!"
|
| 89 |
+
),
|
| 90 |
+
type="messages", # ensures compatibility with Gradio streaming ChatInterface
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.launch()
|