GautamGaur commited on
Commit
ee8cf98
1 Parent(s): c5e863f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''import requests
2
+ import streamlit as st
3
+
4
+ # Replace with the actual URL of your deployed FastAPI backend
5
+ API_URL = "http://127.0.0.1:8000/predict"
6
+
7
+ def main():
8
+ text_input = st.text_input("Enter text to score:")
9
+ if st.button("Score Text"):
10
+ response = requests.post(API_URL, json={"text": text_input})
11
+ data = response.json()
12
+ st.write(f"Score: {data['score']}")
13
+ st.write(f"Message: {data['message']}")
14
+
15
+ if __name__ == "__main__":
16
+ main()'''
17
+
18
+ import streamlit as st
19
+ import torch
20
+ from transformers import RobertaTokenizer, RobertaForSequenceClassification
21
+
22
+ # Load the tokenizer
23
+ tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
24
+
25
+ # Load the model
26
+ model_path = "model_ai_detection"
27
+ model = RobertaForSequenceClassification.from_pretrained(model_path)
28
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
29
+ model.to(device)
30
+ model.eval()
31
+
32
+ def predict(text):
33
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
34
+ inputs = {k: v.to(device) for k, v in inputs.items()}
35
+
36
+ with torch.no_grad():
37
+ outputs = model(**inputs)
38
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
39
+ ai_prob = probs[0][1].item() * 100 # Probability of the text being AI-generated
40
+
41
+ message = "The text is likely generated by AI." if ai_prob > 50 else "The text is likely generated by a human."
42
+
43
+ return {
44
+ "score": ai_prob,
45
+ "message": message
46
+ }
47
+
48
+ def main():
49
+ st.title("AI Text Detector")
50
+ text_input = st.text_area("Enter text to score:")
51
+ if st.button("Score Text"):
52
+ if text_input:
53
+ result = predict(text_input)
54
+ st.write(f"Score: {result['score']:.2f}%")
55
+ st.write(f"Message: {result['message']}")
56
+ else:
57
+ st.write("Please enter some text to score.")
58
+
59
+ if __name__ == "__main__":
60
+ main()
61
+