nashjiwani commited on
Commit
bcd9a29
ยท
verified ยท
1 Parent(s): 844be87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -60
app.py CHANGED
@@ -1,35 +1,30 @@
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):
@@ -38,7 +33,7 @@ def oracle_image(user_img):
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):
@@ -48,10 +43,11 @@ def oracle_audio(audio_file):
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
@@ -59,101 +55,93 @@ 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)
 
1
  import gradio as gr
2
  import spaces
3
  from transformers import pipeline
4
+ import fitz # PyMuPDF for PDF text extraction
5
 
6
  # -------------------------------
7
  # Load Models
8
  # -------------------------------
 
9
  text_detector = pipeline("text-classification", model="roberta-base-openai-detector")
 
 
10
  image_analyzer = pipeline("image-classification", model="microsoft/resnet-50")
 
 
11
  asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-tiny", device=-1)
12
 
13
  # -------------------------------
14
+ # Functions
15
  # -------------------------------
 
16
  @spaces.GPU
17
  def oracle_text(user_text: str):
18
+ if not user_text.strip():
19
+ return "โš ๏ธ Please enter some text."
20
  result = text_detector(user_text)
21
  label = result[0]['label']
22
  score = round(result[0]['score'] * 100, 2)
23
+ return (
24
+ f"๐ŸŒŒ **Prophecy:** {score}% chance this was conjured by AI ๐Ÿค–โœจ"
25
+ if label.lower() == "fake"
26
+ else f"โ˜€๏ธ **Prophecy:** {score}% chance this was crafted by a Human ๐Ÿง‘โ€๐Ÿ’ป"
27
+ )
28
 
29
  @spaces.GPU
30
  def oracle_image(user_img):
 
33
  result = image_analyzer(user_img)
34
  label = result[0]['label']
35
  score = round(result[0]['score'] * 100, 2)
36
+ return f"๐Ÿ–ผ๏ธ Oracleโ€™s Sight: {score}% resemblance to *{label}*\n*(Image prophecy experimental ๐ŸŒŒ)*"
37
 
38
  @spaces.GPU
39
  def oracle_audio(audio_file):
 
43
  result = text_detector(transcript)
44
  label = result[0]['label']
45
  score = round(result[0]['score'] * 100, 2)
46
+ verdict = (
47
+ f"๐Ÿ”Š Transcript: *{transcript}*\n\n๐ŸŒŒ Prophecy: {score}% AIโ€‘generated."
48
+ if label.lower() == "fake"
49
+ else f"๐Ÿ”Š Transcript: *{transcript}*\n\nโ˜€๏ธ Prophecy: {score}% Humanโ€‘written."
50
+ )
51
  return verdict
52
 
53
  @spaces.GPU
 
55
  if pdf_file is None:
56
  return "โš ๏ธ Please upload a PDF."
57
  doc = fitz.open(pdf_file)
58
+ text = "".join(page.get_text() for page in doc)
 
 
59
  if not text.strip():
60
+ return "โš ๏ธ No readable text found in PDF!"
61
+ result = text_detector(text[:800])
 
62
  label = result[0]['label']
63
  score = round(result[0]['score'] * 100, 2)
64
+ return (
65
+ f"๐Ÿ“‘ PDF Prophecy: {score}% AIโ€‘generated."
66
+ if label.lower() == "fake"
67
+ else f"๐Ÿ“‘ PDF Prophecy: {score}% Humanโ€‘authored."
68
+ )
69
 
70
  # -------------------------------
71
  # Gradio Immersive UI
72
  # -------------------------------
73
  with gr.Blocks(css="""
74
  body {
75
+ background: linear-gradient(135deg, #ff9a9e, #fad0c4, #a1c4fd, #c2e9fb);
76
  font-family: 'Trebuchet MS', sans-serif;
77
+ color: #E0FFFF;
78
  }
79
  #title {
80
  text-align: center;
81
+ font-size: 3em;
82
  color: #FFD700;
83
  padding: 20px;
84
+ text-shadow: 3px 3px 10px #000;
85
  }
86
  #subtitle {
87
  text-align: center;
88
+ font-size: 1.4em;
89
+ color: #00FFFF;
90
+ margin-bottom: 30px;
91
  }
92
  .result-box {
93
+ background: rgba(0,0,0,0.6);
94
  border-radius: 15px;
95
  padding: 20px;
96
+ font-size: 1.4em;
97
+ margin-top: 15px;
98
+ color: #FFFFFF;
99
  box-shadow: 0px 0px 15px #FFD700;
100
  }
101
  """) as demo:
102
 
103
  gr.HTML("<div id='title'>๐Ÿ”ฎ Oracle of Truth ๐Ÿ”ฎ</div>")
104
+ gr.HTML("<div id='subtitle'>Multiโ€‘format AI vs Human Divination โœจ Text ยท Image ยท Voice ยท PDF</div>")
 
 
 
105
 
106
+ gr.Markdown("""
107
+ ๐Ÿ‘‹ **Welcome Seeker!**
108
 
109
+ The Oracle peers into your offering and reveals whether it was **AIโ€‘crafted ๐Ÿค–** or **humanโ€‘made ๐Ÿง‘โ€๐Ÿ’ป**.
110
+ Choose one of the formats below โฌ‡๏ธ
 
 
 
111
 
112
+ ๐Ÿ‘‰ Paste text, upload an image, speak aloud, or offer a PDF.
113
  """)
114
 
115
  with gr.Tab("๐Ÿ“œ Text Divination"):
116
+ txt_in = gr.Textbox(lines=8, label="๐Ÿ“ Enter your text", placeholder="Paste your essay, blog, or snippet hereโ€ฆ")
117
  btn_txt = gr.Button("โœจ Reveal Prophecy", variant="primary")
118
  txt_out = gr.Markdown(elem_classes="result-box")
119
  btn_txt.click(oracle_text, inputs=txt_in, outputs=txt_out)
120
 
121
  gr.Examples(
122
  examples=[
123
+ ["Once upon a time in a galaxy far away..."],
124
+ ["Artificial Intelligence is revolutionizing health and education sectors across the globe."],
125
+ ["LOL can't believe it's Monday ๐Ÿ˜…"],
126
+ ["This manuscript provides a comprehensive evaluation of machine translation systems."]
127
  ],
128
  inputs=txt_in
129
  )
130
 
131
  with gr.Tab("๐Ÿ–ผ๏ธ Image Prophecy"):
132
+ img_in = gr.Image(type="filepath", label="๐Ÿ“ท Upload an image")
133
  btn_img = gr.Button("โœจ Reveal Prophecy", variant="primary")
134
  img_out = gr.Markdown(elem_classes="result-box")
135
  btn_img.click(oracle_image, inputs=img_in, outputs=img_out)
136
 
137
  with gr.Tab("๐Ÿ”Š Voice Revelation"):
138
+ aud_in = gr.Audio(sources=["microphone"], type="filepath", label="๐ŸŽค Speak to the Oracle")
139
  btn_aud = gr.Button("โœจ Reveal Prophecy", variant="primary")
140
  aud_out = gr.Markdown(elem_classes="result-box")
141
  btn_aud.click(oracle_audio, inputs=aud_in, outputs=aud_out)
142
 
143
  with gr.Tab("๐Ÿ“‘ PDF Prophecy"):
144
+ pdf_in = gr.File(label="๐Ÿ“„ Upload PDF file", file_types=[".pdf"])
145
  btn_pdf = gr.Button("โœจ Reveal Prophecy", variant="primary")
146
  pdf_out = gr.Markdown(elem_classes="result-box")
147
  btn_pdf.click(oracle_pdf, inputs=pdf_in, outputs=pdf_out)