File size: 1,955 Bytes
ee8cf98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''import requests

import streamlit as st



# Replace with the actual URL of your deployed FastAPI backend

API_URL = "http://127.0.0.1:8000/predict"



def main():

    text_input = st.text_input("Enter text to score:")

    if st.button("Score Text"):

        response = requests.post(API_URL, json={"text": text_input})

        data = response.json()

        st.write(f"Score: {data['score']}")

        st.write(f"Message: {data['message']}")



if __name__ == "__main__":

    main()'''

import streamlit as st
import torch
from transformers import RobertaTokenizer, RobertaForSequenceClassification

# Load the tokenizer
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')

# Load the model
model_path = "model_ai_detection"
model = RobertaForSequenceClassification.from_pretrained(model_path)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()

def predict(text):
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
    inputs = {k: v.to(device) for k, v in inputs.items()}
    
    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
        ai_prob = probs[0][1].item() * 100  # Probability of the text being AI-generated
    
    message = "The text is likely generated by AI." if ai_prob > 50 else "The text is likely generated by a human."
    
    return {
        "score": ai_prob,
        "message": message
    }

def main():
    st.title("AI Text Detector")
    text_input = st.text_area("Enter text to score:")
    if st.button("Score Text"):
        if text_input:
            result = predict(text_input)
            st.write(f"Score: {result['score']:.2f}%")
            st.write(f"Message: {result['message']}")
        else:
            st.write("Please enter some text to score.")

if __name__ == "__main__":
    main()