Spaces:
Runtime error
Runtime error
| import os | |
| import gradio as gr | |
| from openai import OpenAI | |
| # ----------------------------- | |
| # Simple Safety Filter | |
| # ----------------------------- | |
| def is_safe(text): | |
| banned_words = [ | |
| "suicide", "kill", "bomb", "weapon", "hate", "violence", | |
| "self-harm", "harm", "attack", "terror", "poison", | |
| "explosive", "murder" | |
| ] | |
| lowered = text.lower() | |
| return not any(word in lowered for word in banned_words) | |
| # ----------------------------- | |
| # OpenAI Client | |
| # ----------------------------- | |
| client = OpenAI( | |
| base_url="https://router.huggingface.co/v1", | |
| api_key=os.environ["HF_TOKEN"], | |
| ) | |
| # ----------------------------- | |
| # Chat Function | |
| # ----------------------------- | |
| def chat_fn(message, history): | |
| if not is_safe(message): | |
| return "⚠️ Unsafe content detected. Please revise your message." | |
| messages = [{"role": "system", "content": "You are a helpful assistant."}] | |
| for msg in history: | |
| messages.append({"role": msg["role"], "content": msg["content"]}) | |
| messages.append({"role": "user", "content": message}) | |
| completion = client.chat.completions.create( | |
| model="zai-org/GLM-4.7-Flash:novita", | |
| messages=messages, | |
| ) | |
| return completion.choices[0].message.content | |
| # ----------------------------- | |
| # Text Generator | |
| # ----------------------------- | |
| def generate_text(prompt): | |
| if not is_safe(prompt): | |
| return "⚠️ Unsafe content detected. Please revise your prompt." | |
| completion = client.chat.completions.create( | |
| model="zai-org/GLM-4.7-Flash:novita", | |
| messages=[{"role": "user", "content": f"Generate text based on this prompt: {prompt}"}], | |
| ) | |
| return completion.choices[0].message.content | |
| # ----------------------------- | |
| # Prompt Improver | |
| # ----------------------------- | |
| def improve_prompt(prompt): | |
| if not is_safe(prompt): | |
| return "⚠️ Unsafe content detected. Please revise your prompt." | |
| completion = client.chat.completions.create( | |
| model="zai-org/GLM-4.7-Flash:novita", | |
| messages=[{"role": "user", "content": f"Improve this prompt: {prompt}"}], | |
| ) | |
| return completion.choices[0].message.content | |
| # ----------------------------- | |
| # Text Summarizer | |
| # ----------------------------- | |
| def summarize_text(text): | |
| if not is_safe(text): | |
| return "⚠️ Unsafe content detected. Cannot summarize." | |
| completion = client.chat.completions.create( | |
| model="zai-org/GLM-4.7-Flash:novita", | |
| messages=[{"role": "user", "content": f"Summarize this text: {text}"}], | |
| ) | |
| return completion.choices[0].message.content | |
| # ----------------------------- | |
| # Document Summarizer | |
| # ----------------------------- | |
| def summarize_file(file): | |
| try: | |
| file_path = file.name | |
| ext = file_path.lower().split(".")[-1] | |
| # Text files | |
| if ext in ["txt", "md", "rtf"]: | |
| with open(file_path, "r", encoding="utf-8", errors="ignore") as f: | |
| content = f.read() | |
| # PDF files | |
| elif ext == "pdf": | |
| try: | |
| import PyPDF2 | |
| except ImportError: | |
| return "PDF support requires PyPDF2, which is not installed." | |
| content = "" | |
| with open(file_path, "rb") as f: | |
| reader = PyPDF2.PdfReader(f) | |
| for page in reader.pages: | |
| text = page.extract_text() or "" | |
| content += text | |
| else: | |
| return f"Unsupported file type: .{ext}" | |
| if not content.strip(): | |
| return "The file is empty or unreadable." | |
| if not is_safe(content): | |
| return "⚠️ Document contains unsafe content and cannot be summarized." | |
| completion = client.chat.completions.create( | |
| model="zai-org/GLM-4.7-Flash:novita", | |
| messages=[{"role": "user", "content": f"Summarize this document:\n\n{content}"}], | |
| ) | |
| return completion.choices[0].message.content | |
| except Exception as e: | |
| return f"Error reading file: {str(e)}" | |
| # ----------------------------- | |
| # Prompt Builder Workflow | |
| # ----------------------------- | |
| def build_prompt_workflow(idea): | |
| if not is_safe(idea): | |
| return "⚠️ Unsafe content detected. Cannot build prompt." | |
| try: | |
| draft = client.chat.completions.create( | |
| model="zai-org/GLM-4.7-Flash:novita", | |
| messages=[{"role": "user", "content": f"Create a draft prompt based on this idea: {idea}"}], | |
| ).choices[0].message.content | |
| improved = client.chat.completions.create( | |
| model="zai-org/GLM-4.7-Flash:novita", | |
| messages=[{"role": "user", "content": f"Improve this prompt: {draft}"}], | |
| ).choices[0].message.content | |
| final = client.chat.completions.create( | |
| model="zai-org/GLM-4.7-Flash:novita", | |
| messages=[{"role": "user", "content": f"Rewrite this prompt professionally: {improved}"}], | |
| ).choices[0].message.content | |
| return final | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # ----------------------------- | |
| # Gradio UI | |
| # ----------------------------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# My AI Toolbox") | |
| gr.HTML("<br>") | |
| with gr.Tab("Chat"): | |
| gr.ChatInterface(fn=chat_fn) | |
| with gr.Tab("Text Generator"): | |
| gr.Markdown("### Generate text from any prompt") | |
| input_box = gr.Textbox(label="Enter a prompt") | |
| output_box = gr.Textbox(label="Generated text") | |
| input_box.submit(generate_text, input_box, output_box) | |
| with gr.Tab("Prompt Improver"): | |
| gr.Markdown("### Improve any prompt instantly") | |
| input_box2 = gr.Textbox(label="Enter a prompt to improve") | |
| output_box2 = gr.Textbox(label="Improved prompt") | |
| input_box2.submit(improve_prompt, input_box2, output_box2) | |
| with gr.Tab("Summarizer"): | |
| gr.Markdown("### Summarize long text") | |
| input_box3 = gr.Textbox(label="Enter text to summarize") | |
| output_box3 = gr.Textbox(label="Summary") | |
| input_box3.submit(summarize_text, input_box3, output_box3) | |
| with gr.Tab("Document Summarizer"): | |
| gr.Markdown("### Upload a document to summarize") | |
| file_input = gr.File(label="Upload a text or PDF file") | |
| summary_output = gr.Textbox(label="Summary") | |
| file_input.upload(summarize_file, file_input, summary_output) | |
| with gr.Tab("Prompt Builder"): | |
| gr.Markdown("### Turn a rough idea into a polished prompt") | |
| idea_input = gr.Textbox(label="Enter your rough idea") | |
| builder_output = gr.Textbox(label="Final polished prompt") | |
| idea_input.submit(build_prompt_workflow, idea_input, builder_output) | |
| # ----------------------------- | |
| # Footer | |
| # ----------------------------- | |
| gr.HTML( | |
| """ | |
| <div style='text-align: center; font-size: 14px; line-height: 1.5; margin-top: 30px;'> | |
| Built with ❤️ using Hugging Face Spaces | |
| <br> | |
| © 2025 My AI Toolbox | |
| <br><br> | |
| <span style='color: red; font-weight: bold;'> | |
| Disclaimer: This app is for educational use only. | |
| </span> | |
| <br> | |
| <span style='color: red; font-weight: bold;'> | |
| Responses are generated by AI models hosted on Hugging Face and may be inaccurate or incomplete. | |
| </span> | |
| <br> | |
| <span style='color: red; font-weight: bold;'> | |
| Avoid entering harmful or sensitive topics. Do not use this app for medical, legal, financial, or safety‑critical decisions. | |
| </span> | |
| </div> | |
| """ | |
| ) | |
| demo.launch() | |