abhi099k commited on
Commit
f75c14d
·
verified ·
1 Parent(s): 504b65e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +61 -39
src/streamlit_app.py CHANGED
@@ -1,40 +1,62 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # ====================== APP CONFIG ======================
6
+ st.set_page_config(
7
+ page_title="AI Text Detector",
8
+ page_icon="🤖",
9
+ layout="centered"
10
+ )
11
+
12
+ st.title("🧠 AI Text Detector (DeBERTa-v3-large)")
13
+ st.markdown("""
14
+ This tool detects whether the given text is **Human-written** or **AI-generated**
15
+ using a fine-tuned `microsoft/deberta-v3-large` model.
16
+ """)
17
+
18
+ # ====================== LOAD MODEL ======================
19
+ @st.cache_resource
20
+ def load_model():
21
+ model_name = "your-username/ai-text-detector-deberta-v3-large" # Replace with your HF model repo
22
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
23
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
24
+ return tokenizer, model
25
+
26
+ tokenizer, model = load_model()
27
+
28
+ # ====================== TEXT INPUT ======================
29
+ user_text = st.text_area(
30
+ "Enter text to analyze:",
31
+ placeholder="Paste or write any text here...",
32
+ height=200
33
+ )
34
+
35
+ if st.button("🔍 Analyze Text", type="primary"):
36
+ if not user_text.strip():
37
+ st.warning("⚠️ Please enter some text.")
38
+ else:
39
+ with st.spinner("Analyzing..."):
40
+ inputs = tokenizer(user_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
41
+ with torch.no_grad():
42
+ outputs = model(**inputs)
43
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]
44
+ confidence, prediction = torch.max(probs, dim=0)
45
+
46
+ label = "🤖 AI-generated" if prediction.item() == 1 else "🧍 Human-written"
47
+ confidence_percent = confidence.item() * 100
48
+
49
+ st.success(f"**Prediction:** {label}")
50
+ st.progress(confidence.item())
51
+ st.write(f"**Confidence:** {confidence_percent:.2f}%")
52
+
53
+ # Detailed Probabilities
54
+ st.markdown("### 📊 Detailed Probabilities")
55
+ st.write({
56
+ "Human (0)": f"{probs[0].item() * 100:.2f}%",
57
+ "AI (1)": f"{probs[1].item() * 100:.2f}%"
58
+ })
59
+
60
+ # ====================== FOOTER ======================
61
+ st.markdown("---")
62
+ st.caption("Built with ❤️ using [Streamlit](https://streamlit.io) and [Hugging Face Transformers](https://huggingface.co/transformers).")