Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,68 @@
|
|
| 1 |
-
import
|
| 2 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def classify_text(text):
|
| 5 |
if not text.strip():
|
| 6 |
return "Error: Please enter a non-empty text."
|
| 7 |
|
| 8 |
-
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"
|
| 9 |
-
|
| 10 |
prompt = f"""
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
payload = {
|
| 17 |
"inputs": prompt,
|
| 18 |
"parameters": {
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
}
|
| 22 |
}
|
| 23 |
-
|
| 24 |
try:
|
| 25 |
-
response = requests.post(API_URL, json=payload)
|
| 26 |
response.raise_for_status()
|
| 27 |
result = response.json()
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
return str(result)
|
| 34 |
except Exception as e:
|
| 35 |
-
return f"Error: {str(e)}"
|
| 36 |
|
|
|
|
| 37 |
iface = gr.Interface(
|
| 38 |
fn=classify_text,
|
| 39 |
-
inputs=gr.Textbox(lines=5, placeholder="Paste text to classify..."),
|
| 40 |
-
outputs=gr.Textbox(label="Result"),
|
| 41 |
-
title="🤖
|
| 42 |
-
description="
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Hugging Face Inference API settings
|
| 6 |
+
API_URL = "https://api-inference.huggingface.co/models/qwen/Qwen1.5-72B-Chat"
|
| 7 |
+
API_KEY = os.getenv("HF_API_KEY") # Set this in your Hugging Face Space secrets
|
| 8 |
|
| 9 |
def classify_text(text):
|
| 10 |
if not text.strip():
|
| 11 |
return "Error: Please enter a non-empty text."
|
| 12 |
|
|
|
|
|
|
|
| 13 |
prompt = f"""
|
| 14 |
+
You are a professional text analysis specialist. Your job is to classify whether a given text was written by a human or generated by AI.
|
| 15 |
+
You must respond with clear reasoning and high confidence — your answers should be rated 10/10 in accuracy and helpfulness. Be concise, accurate, and structured.
|
| 16 |
|
| 17 |
+
Analyze the following text and return the result in the exact format:
|
| 18 |
+
|
| 19 |
+
Classification: [AI-written / Human-written]
|
| 20 |
+
Reason: [Short but strong explanation — logic, tone, structure, etc.]
|
| 21 |
+
|
| 22 |
+
Text: "{text}"
|
| 23 |
+
Classification:
|
| 24 |
+
Reason:
|
| 25 |
"""
|
| 26 |
+
|
| 27 |
+
headers = {
|
| 28 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 29 |
+
"Content-Type": "application/json"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
payload = {
|
| 33 |
"inputs": prompt,
|
| 34 |
"parameters": {
|
| 35 |
+
"max_new_tokens": 200,
|
| 36 |
+
"temperature": 0.3
|
| 37 |
}
|
| 38 |
}
|
| 39 |
+
|
| 40 |
try:
|
| 41 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 42 |
response.raise_for_status()
|
| 43 |
result = response.json()
|
| 44 |
|
| 45 |
+
# Handle the response format from Hugging Face API
|
| 46 |
+
if isinstance(result, list) and len(result) > 0:
|
| 47 |
+
if 'generated_text' in result[0]:
|
| 48 |
+
return result[0]['generated_text'].strip()
|
| 49 |
+
return result[0].get('text', str(result)).strip()
|
| 50 |
return str(result)
|
| 51 |
except Exception as e:
|
| 52 |
+
return f"Error: {str(e)}\nTip: Make sure the API key is correct and the model is loaded."
|
| 53 |
|
| 54 |
+
# Gradio interface
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=classify_text,
|
| 57 |
+
inputs=gr.Textbox(lines=5, placeholder="Paste text to classify...", label="Text Input"),
|
| 58 |
+
outputs=gr.Textbox(label="Classification Result"),
|
| 59 |
+
title="🤖 AI vs Human Text Classifier (Qwen 72B)",
|
| 60 |
+
description="""Detect if your text is AI-generated or Human-written using Qwen 72B model via Hugging Face API.
|
| 61 |
+
|
| 62 |
+
Note: First request may take 10-20 seconds while the model loads.""",
|
| 63 |
+
allow_flagging="never"
|
| 64 |
)
|
| 65 |
|
| 66 |
+
# Launch the Gradio app
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
iface.launch()
|