nashjiwani commited on
Commit
689097b
ยท
verified ยท
1 Parent(s): 83b63f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -57
app.py CHANGED
@@ -1,83 +1,82 @@
1
  import gradio as gr
2
  import spaces
3
- import torch
4
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
 
6
  # -------------------------------
7
- # Load Qwen2.5-7B (text-only model, works for both Genie & Wizard replies)
8
  # -------------------------------
9
- model_id = "Qwen/Qwen2.5-7B-Instruct"
10
 
11
- tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
12
- model = AutoModelForCausalLM.from_pretrained(
13
- model_id,
14
- torch_dtype=torch.float16,
15
- device_map="auto",
16
- trust_remote_code=True
17
- )
18
 
19
- qwen_pipe = pipeline(
20
- "text-generation",
21
- model=model,
22
- tokenizer=tokenizer,
23
- device_map="auto"
24
- )
25
 
26
  # -------------------------------
27
- # Life Genie: text chat
28
  # -------------------------------
29
- @spaces.GPU
30
- def life_genie(user_input: str) -> str:
31
- if not user_input.strip():
32
- return "๐Ÿงž Please ask me something magical!"
33
- prompt = f"You are a witty magical genie. Answer creatively.\nUser: {user_input}\nGenie:"
34
- output = qwen_pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
35
- return output[0]["generated_text"]
36
 
37
- # -------------------------------
38
- # Whisper ASR for voice input
39
- # -------------------------------
40
- asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-tiny", device=-1)
 
 
 
 
 
 
 
41
 
42
- # -------------------------------
43
- # Voice Wizard: speech โ†’ Genie reply
44
- # -------------------------------
45
  @spaces.GPU
46
- def voice_wizard(audio_file) -> str:
47
- if audio_file is None:
48
- return "๐ŸŽค Please record your voice first!"
49
- # Step 1: Transcribe microphone audio
50
- transcript = asr_pipe(audio_file)["text"]
51
- # Step 2: Ask Genie with that transcript
52
- prompt = f"The user spoke: '{transcript}'. Respond like a magical wizard with wisdom and humor.\nWizard:"
53
- output = qwen_pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
54
- return f"โœจ Voice Transcription: {transcript}\n\n๐Ÿ”ฎ Wizard Replies:\n{output[0]['generated_text']}"
55
 
56
  # -------------------------------
57
  # Gradio UI
58
  # -------------------------------
59
  with gr.Blocks(css="""
60
- body {
61
- background: linear-gradient(135deg, #ff9a9e, #fad0c4);
62
  font-family: Trebuchet MS, sans-serif;
 
63
  }
 
64
  """) as demo:
65
 
66
- gr.HTML("<h1 style='text-align:center; color:white;'>๐ŸŒˆโœจ Magic-Lab โœจ๐ŸŒˆ</h1>")
67
- gr.Markdown("Welcome! Meet the Genie through text or voice. ๐Ÿงž๐Ÿ”ฎ")
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- # Life Genie (Text)
70
- with gr.Tab("๐Ÿงž Life Genie"):
71
- txt_in = gr.Textbox(lines=3, placeholder="Ask me anything...", label="Your Wish")
72
- txt_out = gr.Textbox(lines=10, label="Genie's Response")
73
- btn_chat = gr.Button("โœจ Summon Genie")
74
- btn_chat.click(life_genie, inputs=txt_in, outputs=txt_out)
75
 
76
- # Voice Wizard (Voice)
77
- with gr.Tab("๐Ÿ”ฎ Voice Wizard"):
78
- aud_in = gr.Audio(sources=["microphone"], type="filepath", label="Speak your spell ๐ŸŽค")
79
- aud_out = gr.Textbox(lines=10, label="Wizard's Reply")
80
- btn_voice = gr.Button("๐ŸŽ™๏ธ Cast Spell")
81
- btn_voice.click(voice_wizard, inputs=aud_in, outputs=aud_out)
 
 
 
82
 
83
  demo.launch()
 
1
  import gradio as gr
2
  import spaces
3
+ from transformers import pipeline
 
4
 
5
  # -------------------------------
6
+ # Load lightweight AI detection models
7
  # -------------------------------
 
8
 
9
+ # Text AI detector
10
+ text_detector = pipeline("text-classification", model="roberta-base-openai-detector")
 
 
 
 
 
11
 
12
+ # Image AI detector
13
+ image_detector = pipeline("image-classification", model="oripress/ai-image-detector")
 
 
 
 
14
 
15
  # -------------------------------
16
+ # Functions for Oracle of Truth
17
  # -------------------------------
 
 
 
 
 
 
 
18
 
19
+ @spaces.GPU
20
+ def detect_text(user_text: str):
21
+ if not user_text.strip():
22
+ return "๐Ÿงž Please provide some text for the Oracle to examine."
23
+ result = text_detector(user_text)
24
+ label = result[0]['label']
25
+ score = round(result[0]['score'] * 100, 2)
26
+ if label.lower() == "fake":
27
+ return f"๐ŸŒŒ Oracleโ€™s Vision: {score}% chance this was conjured by AI magic ๐Ÿค–โœจ"
28
+ else:
29
+ return f"โ˜€๏ธ Oracleโ€™s Vision: {score}% chance this was written by a mortal hand ๐Ÿง‘โ€๐Ÿ’ป"
30
 
 
 
 
31
  @spaces.GPU
32
+ def detect_image(user_img):
33
+ if user_img is None:
34
+ return "๐Ÿ–ผ๏ธ Please upload an image for the Oracle to divine."
35
+ result = image_detector(user_img)
36
+ label = result[0]['label']
37
+ score = round(result[0]['score'] * 100, 2)
38
+ return f"๐ŸŒ  Oracleโ€™s Gaze: {score}% chance this is {label}."
 
 
39
 
40
  # -------------------------------
41
  # Gradio UI
42
  # -------------------------------
43
  with gr.Blocks(css="""
44
+ body {
45
+ background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
46
  font-family: Trebuchet MS, sans-serif;
47
+ color: white;
48
  }
49
+ h1, h2, h3 { color: #ffcc00; }
50
  """) as demo:
51
 
52
+ gr.HTML("<h1 style='text-align:center;'>๐Ÿ”ฎ Oracle of Truth ๐Ÿ”ฎ</h1>")
53
+ gr.Markdown("""
54
+ Welcome to the **Chamber of Truth** โœจ.
55
+ Offer your **text** or **image** to the Oracle,
56
+ and it will divine whether it was born of **human hands** โ˜€๏ธ
57
+ or conjured by **AI sorcery** ๐Ÿค–๐ŸŒŒ.
58
+ """)
59
+
60
+ with gr.Tab("๐Ÿ“œ Text Divination"):
61
+ txt_in = gr.Textbox(label="Enter Text", lines=5, placeholder="Paste some text here...")
62
+ btn_txt = gr.Button("๐Ÿ” Ask the Oracle")
63
+ txt_out = gr.Textbox(label="Oracle's Verdict", lines=4)
64
+ btn_txt.click(detect_text, inputs=txt_in, outputs=txt_out)
65
 
66
+ with gr.Tab("๐Ÿ–ผ๏ธ Image Divination"):
67
+ img_in = gr.Image(type="filepath", label="Upload an Image")
68
+ btn_img = gr.Button("๐Ÿ” Ask the Oracle")
69
+ img_out = gr.Textbox(label="Oracle's Verdict", lines=2)
70
+ btn_img.click(detect_image, inputs=img_in, outputs=img_out)
 
71
 
72
+ # Preloaded examples
73
+ gr.Examples(
74
+ examples=[
75
+ ["Once upon a time in a galaxy far away, a hero rose against the odds."],
76
+ ["In this paper, we analyze the performance of Transformer models on natural language inference tasks."]
77
+ ],
78
+ inputs=txt_in,
79
+ label="Try sample texts"
80
+ )
81
 
82
  demo.launch()