Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| class ContentAgentUI: | |
| def __init__(self): | |
| self.agent = None | |
| # Optional: Adjust path if needed for hosted environments | |
| css_path = os.path.join(os.getcwd(), "ui", "styles.css") | |
| self.ca_gui = gr.Blocks(css=css_path if os.path.exists(css_path) else None) | |
| self.sections = [ | |
| self.create_header, | |
| self.create_user_guidance, | |
| self.create_main, | |
| self.create_examples, | |
| self.create_footer, | |
| ] | |
| for section in self.sections: | |
| section() | |
| self.ca_gui.launch() | |
| def call_agent(self, input_text): | |
| try: | |
| if self.agent: | |
| return self.agent.get_response(input_text) | |
| else: | |
| return "Agent not loaded." | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def create_header(self): | |
| with self.ca_gui: | |
| gr.Markdown("# Content Agent") | |
| def create_user_guidance(self): | |
| with self.ca_gui: | |
| gr.Markdown(""" | |
| Please enter text below to get started. The AI Agent will try to determine whether the language is polite and uses the following classification: | |
| - `polite` | |
| - `somewhat polite` | |
| - `neutral` | |
| - `impolite` | |
| Technology: | |
| - App is running `deepseek-ai/DeepSeek-R1-Distill-Qwen-32B` text generation model. | |
| - Agent uses Intel's Polite Guard NLP library tool | |
| - Compute on GCP · Nvidia L4 · 4x GPUs · 96 GB | |
| """) | |
| def create_main(self): | |
| with self.ca_gui: | |
| with gr.Row(): | |
| with gr.Column(): | |
| self.user_input = gr.Textbox(label="Your Input", placeholder="Enter something here...") | |
| self.submit_button = gr.Button("Submit") | |
| self.output = gr.Textbox(label="Content feedback", interactive=False, lines=10, max_lines=20) | |
| # Use bound method with `self` | |
| self.submit_button.click(self.call_agent, inputs=self.user_input, outputs=self.output) | |
| self.user_input.submit(self.call_agent, inputs=self.user_input, outputs=self.output) | |
| def get_example(self): | |
| example_root = os.path.join(os.path.dirname(__file__), "examples") | |
| examples = [] | |
| if os.path.exists(example_root): | |
| example_files = [os.path.join(example_root, f) for f in os.listdir(example_root) if f.endswith(".txt")] | |
| for file_path in example_files: | |
| with open(file_path, "r", encoding="utf-8", errors="ignore") as f: | |
| examples.append(f.read()) | |
| return examples | |
| def create_examples(self): | |
| examples = self.get_example() | |
| with self.ca_gui: | |
| if examples: | |
| example_radio = gr.Radio(choices=examples, label="Try one of these examples:") | |
| example_radio.change(fn=lambda ex: ex, inputs=example_radio, outputs=self.user_input) | |
| else: | |
| gr.Markdown("*No examples found.*") | |
| def create_footer(self): | |
| with self.ca_gui: | |
| gr.Markdown("<div id='footer'>Thanks for trying it out!</div>") | |
| def pass_through_agent(self, agent): | |
| # Assign the agent for future use in `call_agent` | |
| self.agent = agent | |