Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,161 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
from transformers import pipeline
|
|
|
|
| 4 |
|
| 5 |
# -------------------------------
|
| 6 |
-
# Load
|
| 7 |
# -------------------------------
|
|
|
|
| 8 |
text_detector = pipeline("text-classification", model="roberta-base-openai-detector")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# -------------------------------
|
| 11 |
-
# Oracle
|
| 12 |
# -------------------------------
|
|
|
|
| 13 |
@spaces.GPU
|
| 14 |
def oracle_text(user_text: str):
|
| 15 |
if not user_text or not user_text.strip():
|
| 16 |
-
return "
|
| 17 |
-
|
| 18 |
result = text_detector(user_text)
|
| 19 |
label = result[0]['label']
|
| 20 |
score = round(result[0]['score'] * 100, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if label.lower() == "fake":
|
| 23 |
-
verdict = f"๐
|
| 24 |
else:
|
| 25 |
-
verdict = f"โ๏ธ
|
| 26 |
-
|
| 27 |
return verdict
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# -------------------------------
|
| 30 |
# Gradio Immersive UI
|
| 31 |
# -------------------------------
|
| 32 |
with gr.Blocks(css="""
|
| 33 |
body {
|
| 34 |
-
background: linear-gradient(135deg, #
|
| 35 |
font-family: 'Trebuchet MS', sans-serif;
|
| 36 |
color: white;
|
| 37 |
}
|
| 38 |
#title {
|
| 39 |
text-align: center;
|
| 40 |
-
font-size: 2.
|
| 41 |
color: #FFD700;
|
| 42 |
padding: 20px;
|
|
|
|
| 43 |
}
|
| 44 |
#subtitle {
|
| 45 |
text-align: center;
|
| 46 |
font-size: 1.3em;
|
| 47 |
color: #E0FFFF;
|
| 48 |
-
margin-bottom:
|
| 49 |
}
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
""") as demo:
|
| 54 |
|
| 55 |
-
# Title and intro
|
| 56 |
gr.HTML("<div id='title'>๐ฎ Oracle of Truth ๐ฎ</div>")
|
| 57 |
-
gr.HTML("<div id='subtitle'>
|
| 58 |
-
|
| 59 |
-
gr.Markdown(
|
| 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 |
-
gr.
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
from transformers import pipeline
|
| 4 |
+
import fitz # PyMuPDF for PDF extraction
|
| 5 |
|
| 6 |
# -------------------------------
|
| 7 |
+
# Load Models
|
| 8 |
# -------------------------------
|
| 9 |
+
# Text detector (public)
|
| 10 |
text_detector = pipeline("text-classification", model="roberta-base-openai-detector")
|
| 11 |
|
| 12 |
+
# Image classifier (public baseline, since AI-specific detectors are often gated)
|
| 13 |
+
image_analyzer = pipeline("image-classification", model="microsoft/resnet-50")
|
| 14 |
+
|
| 15 |
+
# Whisper tiny for audio โ text transcription
|
| 16 |
+
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-tiny", device=-1)
|
| 17 |
+
|
| 18 |
# -------------------------------
|
| 19 |
+
# Oracle Functions
|
| 20 |
# -------------------------------
|
| 21 |
+
|
| 22 |
@spaces.GPU
|
| 23 |
def oracle_text(user_text: str):
|
| 24 |
if not user_text or not user_text.strip():
|
| 25 |
+
return "โ ๏ธ Please provide some text for the Oracle."
|
|
|
|
| 26 |
result = text_detector(user_text)
|
| 27 |
label = result[0]['label']
|
| 28 |
score = round(result[0]['score'] * 100, 2)
|
| 29 |
+
if label.lower() == "fake":
|
| 30 |
+
return f"๐ **Prophecy:** {score}% chance this was **conjured by AI sorcery** ๐คโจ"
|
| 31 |
+
else:
|
| 32 |
+
return f"โ๏ธ **Prophecy:** {score}% chance this was **crafted by a mortal hand** ๐งโ๐ป"
|
| 33 |
+
|
| 34 |
+
@spaces.GPU
|
| 35 |
+
def oracle_image(user_img):
|
| 36 |
+
if user_img is None:
|
| 37 |
+
return "โ ๏ธ Please upload an image!"
|
| 38 |
+
result = image_analyzer(user_img)
|
| 39 |
+
label = result[0]['label']
|
| 40 |
+
score = round(result[0]['score'] * 100, 2)
|
| 41 |
+
return f"๐ **Oracleโs Gaze:** {score}% resemblance to *{label}*.\n(Note: Image prophecy = experimental ๐)"
|
| 42 |
|
| 43 |
+
@spaces.GPU
|
| 44 |
+
def oracle_audio(audio_file):
|
| 45 |
+
if audio_file is None:
|
| 46 |
+
return "โ ๏ธ Please record your voice."
|
| 47 |
+
transcript = asr_pipe(audio_file)["text"]
|
| 48 |
+
result = text_detector(transcript)
|
| 49 |
+
label = result[0]['label']
|
| 50 |
+
score = round(result[0]['score'] * 100, 2)
|
| 51 |
if label.lower() == "fake":
|
| 52 |
+
verdict = f"๐ Transcript: *{transcript}*\n\n๐ Prophecy: {score}% AI-generated."
|
| 53 |
else:
|
| 54 |
+
verdict = f"๐ Transcript: *{transcript}*\n\nโ๏ธ Prophecy: {score}% Human-written."
|
|
|
|
| 55 |
return verdict
|
| 56 |
|
| 57 |
+
@spaces.GPU
|
| 58 |
+
def oracle_pdf(pdf_file):
|
| 59 |
+
if pdf_file is None:
|
| 60 |
+
return "โ ๏ธ Please upload a PDF."
|
| 61 |
+
doc = fitz.open(pdf_file)
|
| 62 |
+
text = ""
|
| 63 |
+
for page in doc:
|
| 64 |
+
text += page.get_text()
|
| 65 |
+
if not text.strip():
|
| 66 |
+
return "โ ๏ธ Could not extract text from PDF."
|
| 67 |
+
# Detect
|
| 68 |
+
result = text_detector(text[:800]) # limit chars for speed
|
| 69 |
+
label = result[0]['label']
|
| 70 |
+
score = round(result[0]['score'] * 100, 2)
|
| 71 |
+
if label.lower() == "fake":
|
| 72 |
+
return f"๐ PDF Analysis: {score}% chance this was made by AI ๐ค"
|
| 73 |
+
else:
|
| 74 |
+
return f"๐ PDF Analysis: {score}% chance this was human-authored ๐งโ๐ป"
|
| 75 |
+
|
| 76 |
# -------------------------------
|
| 77 |
# Gradio Immersive UI
|
| 78 |
# -------------------------------
|
| 79 |
with gr.Blocks(css="""
|
| 80 |
body {
|
| 81 |
+
background: linear-gradient(135deg, #ff9a9e, #fad0c4, #a18cd1, #fbc2eb);
|
| 82 |
font-family: 'Trebuchet MS', sans-serif;
|
| 83 |
color: white;
|
| 84 |
}
|
| 85 |
#title {
|
| 86 |
text-align: center;
|
| 87 |
+
font-size: 2.8em;
|
| 88 |
color: #FFD700;
|
| 89 |
padding: 20px;
|
| 90 |
+
text-shadow: 2px 2px 5px #000;
|
| 91 |
}
|
| 92 |
#subtitle {
|
| 93 |
text-align: center;
|
| 94 |
font-size: 1.3em;
|
| 95 |
color: #E0FFFF;
|
| 96 |
+
margin-bottom: 20px;
|
| 97 |
}
|
| 98 |
+
.result-box {
|
| 99 |
+
background: rgba(255, 255, 255, 0.2);
|
| 100 |
+
border-radius: 15px;
|
| 101 |
+
padding: 20px;
|
| 102 |
+
font-size: 1.3em;
|
| 103 |
+
color: #fff;
|
| 104 |
+
margin-top: 10px;
|
| 105 |
+
box-shadow: 0px 0px 15px #FFD700;
|
| 106 |
}
|
| 107 |
""") as demo:
|
| 108 |
|
|
|
|
| 109 |
gr.HTML("<div id='title'>๐ฎ Oracle of Truth ๐ฎ</div>")
|
| 110 |
+
gr.HTML("<div id='subtitle'>Offer your text, image, voice, or PDF to the Oracle โจ</div>")
|
| 111 |
+
|
| 112 |
+
gr.Markdown("""
|
| 113 |
+
Welcome, seeker ๐
|
| 114 |
+
|
| 115 |
+
The **Oracle of Truth** gazes upon your offering and divines whether it was born of
|
| 116 |
+
a **mortal hand** โ๏ธ or conjured by **AI sorcery** ๐คโจ.
|
| 117 |
+
|
| 118 |
+
## ๐ How to Use
|
| 119 |
+
- ๐ Paste text in the **Text Divination** panel.
|
| 120 |
+
- ๐ผ๏ธ Upload images in the **Image Prophecy** panel.
|
| 121 |
+
- ๐ Speak into the **Voice Revelation** panel.
|
| 122 |
+
- ๐ Offer a **PDF** to see if it was written by human or AI.
|
| 123 |
+
|
| 124 |
+
The Oracle responds in glowing prophecy form โ watch carefully ๐๏ธ
|
| 125 |
+
""")
|
| 126 |
+
|
| 127 |
+
with gr.Tab("๐ Text Divination"):
|
| 128 |
+
txt_in = gr.Textbox(lines=10, label="Your Text โ๏ธ", placeholder="Paste story, essay, or blogโฆ")
|
| 129 |
+
btn_txt = gr.Button("โจ Reveal Prophecy", variant="primary")
|
| 130 |
+
txt_out = gr.Markdown(elem_classes="result-box")
|
| 131 |
+
btn_txt.click(oracle_text, inputs=txt_in, outputs=txt_out)
|
| 132 |
+
|
| 133 |
+
gr.Examples(
|
| 134 |
+
examples=[
|
| 135 |
+
["Once upon a time in a galaxyโฆ"],
|
| 136 |
+
["AI is revolutionizing the world of education and technology."],
|
| 137 |
+
["OMG I canโt believe itโs already Monday ๐
"],
|
| 138 |
+
["This paper provides an extensive overview of machine translation performance."]
|
| 139 |
+
],
|
| 140 |
+
inputs=txt_in
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
with gr.Tab("๐ผ๏ธ Image Prophecy"):
|
| 144 |
+
img_in = gr.Image(type="filepath", label="Upload Image ๐ผ๏ธ")
|
| 145 |
+
btn_img = gr.Button("โจ Reveal Prophecy", variant="primary")
|
| 146 |
+
img_out = gr.Markdown(elem_classes="result-box")
|
| 147 |
+
btn_img.click(oracle_image, inputs=img_in, outputs=img_out)
|
| 148 |
+
|
| 149 |
+
with gr.Tab("๐ Voice Revelation"):
|
| 150 |
+
aud_in = gr.Audio(sources=["microphone"], type="filepath", label="Speak Now ๐๏ธ")
|
| 151 |
+
btn_aud = gr.Button("โจ Reveal Prophecy", variant="primary")
|
| 152 |
+
aud_out = gr.Markdown(elem_classes="result-box")
|
| 153 |
+
btn_aud.click(oracle_audio, inputs=aud_in, outputs=aud_out)
|
| 154 |
+
|
| 155 |
+
with gr.Tab("๐ PDF Prophecy"):
|
| 156 |
+
pdf_in = gr.File(label="Upload PDF ๐", file_types=[".pdf"])
|
| 157 |
+
btn_pdf = gr.Button("โจ Reveal Prophecy", variant="primary")
|
| 158 |
+
pdf_out = gr.Markdown(elem_classes="result-box")
|
| 159 |
+
btn_pdf.click(oracle_pdf, inputs=pdf_in, outputs=pdf_out)
|
| 160 |
|
| 161 |
demo.launch()
|