Spaces:
Sleeping
Sleeping
File size: 4,810 Bytes
a532053 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
import gradio as gr
import os
from expressly_server.crew import ExpresslyServer
def call(prompt, target_audience, format, tone, active_tab):
"""
Calls the Expressly Server API to generate content based on the given inputs.
Args:
prompt (str): The text prompt for the chat.
target_audience (str): The target audience for the response.
format (str): The format of the response, e.g., text, markdown.
tone (str): The tone of the response, e.g., formal, informal.
active_tab (str): The active tab on the UI, either "target_audience" or "format_tone".
Returns:
str: The generated text response.
Raises:
ValueError: If prompt is empty, or if the active_tab value is invalid.
"""
# Validating and constructing the inputs
if prompt is None or prompt == "":
raise ValueError("Prompt is required")
if active_tab == "target_audience":
format = ""
tone = ""
elif active_tab == "format_tone":
target_audience = ""
else:
raise ValueError("Invalid active_tab value")
inputs = {
"prompt": prompt,
"target_audience": target_audience,
"format": format,
"tone": tone,
}
outputs = ExpresslyServer().crew().kickoff(inputs=inputs)
if outputs is None:
result = "Please check the inputs and try again. If the issue persists, contact support."
else:
result = outputs.raw
return result
with gr.Blocks() as app:
gr.Markdown("# Expressly - Text Transformation App")
with gr.Row():
# Left Column for Inputs
with gr.Column(scale=1):
prompt = gr.Textbox(label="Message Expressly", max_length=1024, lines=3)
# Create a state variable to store active tab
active_tab = gr.State("target_audience")
with gr.Tab("Target Audience", id="tab_audience") as tab1:
target_audience = gr.Dropdown(
[
"LinkedIn Post",
"WhatsApp Message",
"Tweet",
"News Article",
"Technical Blog",
"Formal Email",
"Instagram Post",
"Website Content",
"Marketing Email",
"Job Application",
"Customer Support Response",
],
label="Target Audience",
info="Pick a Target Audience to specify the purpose or platform.",
)
# Update state when this tab is selected
tab1.select(lambda: "target_audience", None, active_tab)
with gr.Tab("Format & Tone", id="tab_format") as tab2:
format = gr.Dropdown(
[
"Post",
"Chat",
"Tweet",
"Email",
"Blog",
"Article",
"Report",
"Product Description",
],
label="Format",
info="Choose a Format to define the type of content.",
)
tone = gr.Dropdown(
[
"Professional",
"Casual",
"Straightforward",
"Confident",
"Friendly",
"Neutral",
"Storytelling",
"Inspirational",
],
label="Tone",
info="Select a Tone to set the communication style.",
)
# Update state when this tab is selected
tab2.select(lambda: "format_tone", None, active_tab)
btn_submit = gr.Button("Submit")
# Right Column for Output
with gr.Column(scale=1):
results = gr.Markdown(label="Result")
btn_submit.click(
fn=call,
inputs=[prompt, target_audience, format, tone, active_tab],
outputs=[results],
)
def launch():
"""
Launch the Expressly web app.
This function starts the Expressly web app in a threaded mode, allowing it
to process multiple requests concurrently. The app is configured to
accept up to 10 requests in its queue at any given time.
The app is launched with strict CORS checks disabled, which allows it
to be accessed from any origin.
To launch the app, call this function with no arguments.
Example:
launch()
"""
app.queue(max_size=10).launch(strict_cors=False)
if __name__ == "__main__":
launch()
|