Humaira81 commited on
Commit
e647db2
·
verified ·
1 Parent(s): 0a7d5b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -36
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
 
3
  from deep_translator import GoogleTranslator
4
 
5
  # Load models
@@ -7,51 +8,54 @@ humanizer = pipeline("text2text-generation", model="humarin/chatgpt_paraphraser_
7
  detector_model = AutoModelForSequenceClassification.from_pretrained("roberta-base-openai-detector")
8
  detector_tokenizer = AutoTokenizer.from_pretrained("roberta-base-openai-detector")
9
 
10
- # Inference Functions
11
- def rewrite_text(input_text, tone):
 
 
 
 
12
  prefix_map = {
13
- "Academic": "paraphrase this academically: ",
14
- "Professional": "rephrase this professionally: ",
15
- "Conversational": "make this sound casual: "
16
  }
17
  prefix = prefix_map.get(tone, "")
18
- response = humanizer(prefix + input_text, max_length=512, do_sample=False)[0]['generated_text']
19
- return response
 
 
20
 
21
- def detect_ai_content(text):
22
- inputs = detector_tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
23
  outputs = detector_model(**inputs)
24
- score = float(outputs.logits.softmax(dim=1)[0][1]) # AI-likeness score
25
  if score > 0.75:
26
- verdict = "⚠️ This seems AI-generated (score: {:.2f})".format(score)
27
  elif score > 0.5:
28
- verdict = "❓ Possibly AI-written (score: {:.2f})".format(score)
 
 
 
 
 
 
 
 
29
  else:
30
- verdict = "✅ Human-like writing (score: {:.2f})".format(score)
31
- return verdict
32
 
33
- def translate_text(text, target_lang):
34
- try:
35
- if target_lang == "Bahasa Malaysia":
36
- translated = GoogleTranslator(source='auto', target='ms').translate(text)
37
- elif target_lang == "Chinese":
38
- translated = GoogleTranslator(source='auto', target='zh-CN').translate(text)
39
- else:
40
- translated = GoogleTranslator(source='auto', target='en').translate(text)
41
- return translated
42
- except Exception as e:
43
- return f"Translation error: {str(e)}"
44
 
45
  # Gradio UI
46
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
47
  gr.Markdown("### 🕌 Zawiyah AI Collective – Humanizer AI")
48
- gr.Markdown("Transforms text into Academic, Professional, or Conversational style, detects AI content, and translates to Bahasa Malaysia or Chinese.")
49
 
50
  with gr.Row():
51
  with gr.Column():
52
  input_text = gr.Textbox(label="📝 Enter your text", lines=8, placeholder="Paste your content here...")
53
  tone = gr.Radio(["Academic", "Professional", "Conversational"], label="Select Tone", value="Academic")
54
- lang = gr.Radio(["Bahasa Malaysia", "Chinese"], label="Translate To", value="Bahasa Malaysia")
55
  submit_btn = gr.Button("🚀 Submit")
56
  clear_btn = gr.Button("🧹 Clear")
57
  with gr.Column():
@@ -59,16 +63,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
59
  detection = gr.Textbox(label="🤖 AI Detection Result")
60
  translated = gr.Textbox(label="🌐 Translated Version")
61
 
62
- # Button click events
63
- submit_btn.click(fn=rewrite_text, inputs=[input_text, tone], outputs=rewritten)
64
- submit_btn.click(fn=detect_ai_content, inputs=input_text, outputs=detection)
65
- submit_btn.click(fn=translate_text, inputs=[input_text, lang], outputs=translated)
66
- clear_btn.click(lambda: ("", "", "", ""), outputs=[input_text, rewritten, detection, translated])
67
 
68
  gr.Markdown("---")
69
- gr.Markdown(
70
- "☕ [Buy Me a Coffee](https://www.buymeacoffee.com/humairaai) to support future updates! | Built with ❤️ using [Gradio](https://gradio.app)"
71
- )
72
 
73
  # Launch
74
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
  from deep_translator import GoogleTranslator
5
 
6
  # Load models
 
8
  detector_model = AutoModelForSequenceClassification.from_pretrained("roberta-base-openai-detector")
9
  detector_tokenizer = AutoTokenizer.from_pretrained("roberta-base-openai-detector")
10
 
11
+ # Core logic
12
+ def process_text(input_text, tone, lang):
13
+ if not input_text.strip():
14
+ return "", "❌ No input text provided", ""
15
+
16
+ # Tone-based prefix
17
  prefix_map = {
18
+ "Academic": "Paraphrase this academically: ",
19
+ "Professional": "Rephrase this professionally: ",
20
+ "Conversational": "Make this sound casual: "
21
  }
22
  prefix = prefix_map.get(tone, "")
23
+ prompt = prefix + input_text.strip()
24
+
25
+ # Humanize
26
+ humanized = humanizer(prompt, max_length=512, do_sample=False)[0]['generated_text']
27
 
28
+ # Detect AI likelihood
29
+ inputs = detector_tokenizer(humanized, return_tensors="pt", truncation=True, max_length=512)
30
  outputs = detector_model(**inputs)
31
+ score = float(outputs.logits.softmax(dim=1)[0][1])
32
  if score > 0.75:
33
+ verdict = f"⚠️ This seems AI-generated (score: {score:.2f})"
34
  elif score > 0.5:
35
+ verdict = f"❓ Possibly AI-written (score: {score:.2f})"
36
+ else:
37
+ verdict = f"✅ Human-like writing (score: {score:.2f})"
38
+
39
+ # Translate
40
+ if lang == "Bahasa Malaysia":
41
+ translated = GoogleTranslator(source='auto', target='ms').translate(humanized)
42
+ elif lang == "Chinese":
43
+ translated = GoogleTranslator(source='auto', target='zh-CN').translate(humanized)
44
  else:
45
+ translated = "❎ Translation skipped (None selected)"
 
46
 
47
+ return humanized, verdict, translated
 
 
 
 
 
 
 
 
 
 
48
 
49
  # Gradio UI
50
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
51
  gr.Markdown("### 🕌 Zawiyah AI Collective – Humanizer AI")
52
+ gr.Markdown("Transforms input into refined **Academic**, **Professional**, or **Conversational** tone. Also detects AI likelihood and optionally translates.")
53
 
54
  with gr.Row():
55
  with gr.Column():
56
  input_text = gr.Textbox(label="📝 Enter your text", lines=8, placeholder="Paste your content here...")
57
  tone = gr.Radio(["Academic", "Professional", "Conversational"], label="Select Tone", value="Academic")
58
+ lang = gr.Radio(["None", "Bahasa Malaysia", "Chinese"], label="Translate To", value="None")
59
  submit_btn = gr.Button("🚀 Submit")
60
  clear_btn = gr.Button("🧹 Clear")
61
  with gr.Column():
 
63
  detection = gr.Textbox(label="🤖 AI Detection Result")
64
  translated = gr.Textbox(label="🌐 Translated Version")
65
 
66
+ submit_btn.click(fn=process_text, inputs=[input_text, tone, lang], outputs=[rewritten, detection, translated])
67
+ clear_btn.click(lambda: ("", "", ""), outputs=[rewritten, detection, translated])
 
 
 
68
 
69
  gr.Markdown("---")
70
+ gr.Markdown("☕ [Buy Me a Coffee](https://www.buymeacoffee.com/humairaai) to support future updates | Built with ❤️ using [Gradio](https://gradio.app)")
 
 
71
 
72
  # Launch
73
  demo.launch()