Spaces:
Sleeping
Sleeping
Update ContentGradio.py
Browse files- ContentGradio.py +57 -98
ContentGradio.py
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
|
| 5 |
class ContentAgentUI:
|
| 6 |
def __init__(self):
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
css_path = os.path.join(os.getcwd(), "ui", "styles.css")
|
| 9 |
-
|
| 10 |
-
self.ca_gui = gr.Blocks(css=css_path)
|
| 11 |
-
|
| 12 |
self.sections = [
|
| 13 |
self.create_header,
|
| 14 |
self.create_user_guidance,
|
|
@@ -16,123 +17,81 @@ class ContentAgentUI:
|
|
| 16 |
self.create_examples,
|
| 17 |
self.create_footer,
|
| 18 |
]
|
| 19 |
-
|
| 20 |
for section in self.sections:
|
| 21 |
section()
|
| 22 |
-
|
| 23 |
self.ca_gui.launch()
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def create_header(self):
|
| 26 |
-
agent_header = """
|
| 27 |
-
#Content Agent
|
| 28 |
-
"""
|
| 29 |
with self.ca_gui:
|
| 30 |
-
gr.Markdown(
|
| 31 |
|
| 32 |
def create_user_guidance(self):
|
| 33 |
-
guidance = """
|
| 34 |
-
Please enter text below to get started. The AI Agent will try to determine whether the language is polite and uses the following classification:
|
| 35 |
-
- `polite`
|
| 36 |
-
- `somewhat polite`
|
| 37 |
-
- `neutral`
|
| 38 |
-
- `impolite`
|
| 39 |
-
App is running `deepseek-ai/DeepSeek-R1-Distill-Qwen-32B` text generation model.
|
| 40 |
-
Uses Intel's Polite Guard NLP library.
|
| 41 |
-
Compute is GCP · Nvidia L4 · 4x GPUs · 96 GB
|
| 42 |
-
"""
|
| 43 |
with self.ca_gui:
|
| 44 |
-
gr.Markdown(
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def create_main(self):
|
| 47 |
with self.ca_gui:
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column():
|
| 50 |
self.user_input = gr.Textbox(label="Your Input", placeholder="Enter something here...")
|
| 51 |
self.submit_button = gr.Button("Submit")
|
| 52 |
-
self.output = gr.Textbox(label="Content feedback", interactive=False, lines=10, max_lines=20
|
| 53 |
-
|
| 54 |
-
#
|
| 55 |
-
self.submit_button.click(
|
| 56 |
-
self.user_input.submit(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# Function to generate predefined examples
|
| 60 |
-
def get_example():
|
| 61 |
-
# Define the path to the 'examples' directory
|
| 62 |
example_root = os.path.join(os.path.dirname(__file__), "examples")
|
| 63 |
-
|
| 64 |
-
# Get list of all example text file paths
|
| 65 |
-
example_files = [os.path.join(example_root, _) for _ in os.listdir(example_root) if _.endswith("txt")]
|
| 66 |
-
|
| 67 |
-
# Read the content of each file (assuming they're plain text files)
|
| 68 |
examples = []
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
return examples
|
| 78 |
|
| 79 |
def create_examples(self):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
print("examples")
|
| 83 |
-
print(examples)
|
| 84 |
-
|
| 85 |
-
example_radio = gr.Radio(choices=examples, label="Try one of these examples:")
|
| 86 |
-
|
| 87 |
-
# When an example is selected, populate the input field
|
| 88 |
with self.ca_gui:
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
def create_footer(self):
|
| 92 |
with self.ca_gui:
|
| 93 |
gr.Markdown("<div id='footer'>Thanks for trying it out!</div>")
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
# Function for Main content (takes user input and returns a response)
|
| 98 |
-
def process_input(input_text):
|
| 99 |
-
#return f"You entered: {user_input}"
|
| 100 |
-
|
| 101 |
-
#def get_agent_response(input_text):
|
| 102 |
-
try:
|
| 103 |
-
# Pass the input to the agent
|
| 104 |
-
output = agent.get_response(input_text)
|
| 105 |
-
|
| 106 |
-
# Return the agent's response
|
| 107 |
-
return output
|
| 108 |
-
except Exception as e:
|
| 109 |
-
# Handle any errors that occur
|
| 110 |
-
return f"Error: {str(e)}"
|
| 111 |
-
|
| 112 |
-
self.user_input.change(
|
| 113 |
-
fn=get_agent_response,
|
| 114 |
-
inputs=self.user_input,
|
| 115 |
-
outputs=self.output
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
def pass_through_agent(self, agent):
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
self.output.update(agent_response)
|
| 122 |
-
|
| 123 |
-
# Pass the input to the agent
|
| 124 |
-
output = agent.get_response(input_text)
|
| 125 |
-
|
| 126 |
-
# Update the output text box with the agent's response
|
| 127 |
-
self.submit_button.click(
|
| 128 |
-
fn=process_input,
|
| 129 |
-
inputs=self.user_input,
|
| 130 |
-
outputs=self.output
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
self.user_input.submit(
|
| 134 |
-
fn=get_agent_response,
|
| 135 |
-
inputs=self.user_input,
|
| 136 |
-
outputs=self.output
|
| 137 |
-
)
|
| 138 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
|
|
|
|
| 4 |
class ContentAgentUI:
|
| 5 |
def __init__(self):
|
| 6 |
+
self.agent = None
|
| 7 |
+
|
| 8 |
+
# Optional: Adjust path if needed for hosted environments
|
| 9 |
css_path = os.path.join(os.getcwd(), "ui", "styles.css")
|
| 10 |
+
|
| 11 |
+
self.ca_gui = gr.Blocks(css=css_path if os.path.exists(css_path) else None)
|
| 12 |
+
|
| 13 |
self.sections = [
|
| 14 |
self.create_header,
|
| 15 |
self.create_user_guidance,
|
|
|
|
| 17 |
self.create_examples,
|
| 18 |
self.create_footer,
|
| 19 |
]
|
| 20 |
+
|
| 21 |
for section in self.sections:
|
| 22 |
section()
|
| 23 |
+
|
| 24 |
self.ca_gui.launch()
|
| 25 |
|
| 26 |
+
def call_agent(self, input_text):
|
| 27 |
+
try:
|
| 28 |
+
if self.agent:
|
| 29 |
+
return self.agent.get_response(input_text)
|
| 30 |
+
else:
|
| 31 |
+
return "Agent not loaded."
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error: {str(e)}"
|
| 34 |
+
|
| 35 |
def create_header(self):
|
|
|
|
|
|
|
|
|
|
| 36 |
with self.ca_gui:
|
| 37 |
+
gr.Markdown("# Content Agent")
|
| 38 |
|
| 39 |
def create_user_guidance(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
with self.ca_gui:
|
| 41 |
+
gr.Markdown("""
|
| 42 |
+
Please enter text below to get started. The AI Agent will try to determine whether the language is polite and uses the following classification:
|
| 43 |
+
|
| 44 |
+
- `polite`
|
| 45 |
+
- `somewhat polite`
|
| 46 |
+
- `neutral`
|
| 47 |
+
- `impolite`
|
| 48 |
+
|
| 49 |
+
Technology:
|
| 50 |
+
|
| 51 |
+
- App is running `deepseek-ai/DeepSeek-R1-Distill-Qwen-32B` text generation model.
|
| 52 |
+
- Agent uses Intel's Polite Guard NLP library tool
|
| 53 |
+
- Compute on GCP · Nvidia L4 · 4x GPUs · 96 GB
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
def create_main(self):
|
| 57 |
with self.ca_gui:
|
| 58 |
with gr.Row():
|
| 59 |
with gr.Column():
|
| 60 |
self.user_input = gr.Textbox(label="Your Input", placeholder="Enter something here...")
|
| 61 |
self.submit_button = gr.Button("Submit")
|
| 62 |
+
self.output = gr.Textbox(label="Content feedback", interactive=False, lines=10, max_lines=20)
|
| 63 |
+
|
| 64 |
+
# Use bound method with `self`
|
| 65 |
+
self.submit_button.click(self.call_agent, inputs=self.user_input, outputs=self.output)
|
| 66 |
+
self.user_input.submit(self.call_agent, inputs=self.user_input, outputs=self.output)
|
| 67 |
+
|
| 68 |
+
def get_example(self):
|
|
|
|
|
|
|
|
|
|
| 69 |
example_root = os.path.join(os.path.dirname(__file__), "examples")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
examples = []
|
| 71 |
+
|
| 72 |
+
if os.path.exists(example_root):
|
| 73 |
+
example_files = [os.path.join(example_root, f) for f in os.listdir(example_root) if f.endswith(".txt")]
|
| 74 |
+
|
| 75 |
+
for file_path in example_files:
|
| 76 |
+
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 77 |
+
examples.append(f.read())
|
| 78 |
+
|
| 79 |
return examples
|
| 80 |
|
| 81 |
def create_examples(self):
|
| 82 |
+
examples = self.get_example()
|
| 83 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
with self.ca_gui:
|
| 85 |
+
if examples:
|
| 86 |
+
example_radio = gr.Radio(choices=examples, label="Try one of these examples:")
|
| 87 |
+
example_radio.change(fn=lambda ex: ex, inputs=example_radio, outputs=self.user_input)
|
| 88 |
+
else:
|
| 89 |
+
gr.Markdown("*No examples found.*")
|
| 90 |
|
| 91 |
def create_footer(self):
|
| 92 |
with self.ca_gui:
|
| 93 |
gr.Markdown("<div id='footer'>Thanks for trying it out!</div>")
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
def pass_through_agent(self, agent):
|
| 96 |
+
# Assign the agent for future use in `call_agent`
|
| 97 |
+
self.agent = agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|