| import os |
| import gradio as gr |
| from openai import OpenAI |
|
|
| client = OpenAI( |
| base_url="https://router.huggingface.co/v1", |
| api_key=os.environ["HF_TOKEN"], |
| ) |
|
|
| |
| 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) |
|
|
|
|
| |
| |
| |
| def chat_fn(message, history): |
| if not is_safe(message): |
| return "⚠️ This request may be unsafe. Please try a different topic." |
|
|
| 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 |
|
|
|
|
| |
| |
| |
| def generate_text(prompt): |
| if not is_safe(prompt): |
| return "⚠️ This request may be unsafe. Please try a different topic." |
|
|
| 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 |
|
|
|
|
| |
| |
| |
| def improve_prompt(prompt): |
| if not is_safe(prompt): |
| return "⚠️ This request may be unsafe. Please try a different topic." |
|
|
| 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 |
|
|
|
|
| |
| |
| |
| def summarize_text(text): |
| if not is_safe(text): |
| return "⚠️ This request may be unsafe. Please try a different topic." |
|
|
| 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 |
|
|
|
|
| |
| |
| |
| def summarize_file(file): |
| try: |
| file_path = file.name |
| ext = file_path.lower().split(".")[-1] |
|
|
| |
| if ext in ["txt", "md", "rtf"]: |
| with open(file_path, "r", encoding="utf-8", errors="ignore") as f: |
| content = f.read() |
|
|
| elif ext == "pdf": |
| import PyPDF2 |
| content = "" |
| with open(file_path, "rb") as f: |
| reader = PyPDF2.PdfReader(f) |
| for page in reader.pages: |
| content += page.extract_text() or "" |
|
|
| else: |
| return f"Unsupported file type: .{ext}" |
|
|
| if not content.strip(): |
| return "The file is empty or unreadable." |
|
|
| |
| if not is_safe(content): |
| return "⚠️ This document may contain unsafe content." |
|
|
| 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)}" |
|
|
|
|
| |
| |
| |
| def build_prompt_workflow(idea): |
| if not is_safe(idea): |
| return "⚠️ This request may be unsafe. Please try a different topic." |
|
|
| 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 in a polished, professional way: {improved}"}], |
| ).choices[0].message.content |
|
|
| return final |
|
|
| except Exception as e: |
| return f"Error: {str(e)}" |
|
|
|
|
| |
| |
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# My AI Toolbox") |
| gr.HTML("<br>") |
|
|
| gr.HTML( |
| """ |
| <div style='text-align: center; font-size: 14px; line-height: 1.5;'> |
| 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> |
| |
| |
| """ |
| ) |
|
|
| 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) |
|
|
| demo.launch() |
|
|