File size: 7,318 Bytes
d7d8b89 c5ab826 893c500 bb85623 893c500 a9a7a43 c5ab826 f990e5e 893c500 d7d8b89 a29ffd1 d7d8b89 c5ab826 d7d8b89 c5ab826 a0a9eeb 562aacd c5ab826 4877797 c5ab826 893c500 c5ab826 4877797 c5ab826 4877797 c5ab826 4877797 c5ab826 893c500 c5ab826 4877797 c5ab826 4877797 c5ab826 4877797 c5ab826 893c500 c5ab826 4877797 7770e42 4877797 c5ab826 2b5f93b 73dd166 5e74ddc c5ab826 5e74ddc 73dd166 5e74ddc c5ab826 73dd166 932fa67 73dd166 932fa67 5e74ddc c5ab826 5fe52ce c5ab826 893c500 c5ab826 5fe52ce c5ab826 c664878 c5ab826 beb9416 c5ab826 beb9416 c5ab826 81bfe3b 1cb6c2b 81bfe3b 1cb6c2b 81bfe3b 1cb6c2b c5ab826 81bfe3b 1cb6c2b c5ab826 c664878 c5ab826 3b3f875 c5ab826 a76b091 a9a7a43 a0a9eeb | 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | 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"],
)
# Simple safety filter for dangerous prompts
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)
# -----------------------------
# Chat
# -----------------------------
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
# -----------------------------
# Text Generator
# -----------------------------
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
# -----------------------------
# Prompt Improver
# -----------------------------
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
# -----------------------------
# Text Summarizer
# -----------------------------
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
# -----------------------------
# Document Summarizer
# -----------------------------
def summarize_file(file):
try:
file_path = file.name
ext = file_path.lower().split(".")[-1]
# Read file
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."
# Safety check on extracted text
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)}"
# -----------------------------
# Prompt Workflow Builder
# -----------------------------
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)}"
# -----------------------------
# UI
# -----------------------------
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()
|