Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- requirements.txt +6 -0
- streamlit_app.py +101 -0
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
transformers==4.40.1
|
3 |
+
torch
|
4 |
+
pytesseract
|
5 |
+
Pillow
|
6 |
+
newspaper3k
|
streamlit_app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false"
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
7 |
+
import pytesseract
|
8 |
+
from PIL import Image
|
9 |
+
import difflib
|
10 |
+
from newspaper import Article
|
11 |
+
|
12 |
+
model_name = "google/gemma-2b-it"
|
13 |
+
|
14 |
+
@st.cache_resource
|
15 |
+
def load_model():
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
18 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
19 |
+
return pipe
|
20 |
+
|
21 |
+
pipe = load_model()
|
22 |
+
|
23 |
+
st.set_page_config(page_title="Blog/Image Reviewer", layout="centered")
|
24 |
+
st.title("π§ BlogChecker AI")
|
25 |
+
st.markdown("Paste blog text, upload an image, or give a URL β the AI will review and improve it.")
|
26 |
+
|
27 |
+
mode = st.radio("Choose Input Mode:", ["Text", "Image", "URL"])
|
28 |
+
input_text = ""
|
29 |
+
|
30 |
+
if mode == "Text":
|
31 |
+
input_text = st.text_area("Paste your blog here:", height=300)
|
32 |
+
|
33 |
+
elif mode == "Image":
|
34 |
+
uploaded_image = st.file_uploader("Upload an image with text", type=["png", "jpg", "jpeg"])
|
35 |
+
if uploaded_image is not None:
|
36 |
+
image = Image.open(uploaded_image)
|
37 |
+
input_text = pytesseract.image_to_string(image)
|
38 |
+
st.image(image, caption="Uploaded Image", width=400)
|
39 |
+
st.markdown("**Extracted Text:**")
|
40 |
+
st.text_area("OCR Text:", value=input_text, height=200)
|
41 |
+
|
42 |
+
elif mode == "URL":
|
43 |
+
blog_url = st.text_input("Paste blog/article URL:")
|
44 |
+
if blog_url:
|
45 |
+
try:
|
46 |
+
article = Article(blog_url)
|
47 |
+
article.download()
|
48 |
+
article.parse()
|
49 |
+
input_text = article.text
|
50 |
+
st.success("β
Blog content extracted successfully.")
|
51 |
+
st.text_area("Extracted Blog Content:", value=input_text, height=300)
|
52 |
+
except:
|
53 |
+
st.error("β οΈ Failed to extract content from URL.")
|
54 |
+
|
55 |
+
def generate_review(original):
|
56 |
+
prompt = f"""
|
57 |
+
You are an AI blog reviewer. Improve the content below:
|
58 |
+
- Fix unclear, biased, or emotional language
|
59 |
+
- Correct grammar and spelling
|
60 |
+
- Identify and address sensitive or policy-violating content
|
61 |
+
- Suggest better alternatives
|
62 |
+
- Keep useful content unchanged
|
63 |
+
|
64 |
+
Blog:
|
65 |
+
{original}
|
66 |
+
"""
|
67 |
+
response = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
|
68 |
+
improved = response[0]['generated_text'].split("Blog:")[-1].strip()
|
69 |
+
return improved
|
70 |
+
|
71 |
+
def highlight_changes(original, improved):
|
72 |
+
d = difflib.Differ()
|
73 |
+
diff = list(d.compare(original.split(), improved.split()))
|
74 |
+
highlighted = ""
|
75 |
+
for word in diff:
|
76 |
+
if word.startswith("- "):
|
77 |
+
highlighted += f"~~{word[2:]}~~ "
|
78 |
+
elif word.startswith("+ "):
|
79 |
+
highlighted += f"**{word[2:]}** "
|
80 |
+
elif word.startswith(" "):
|
81 |
+
highlighted += word[2:] + " "
|
82 |
+
return highlighted
|
83 |
+
|
84 |
+
if st.button("π Review Content"):
|
85 |
+
if input_text.strip():
|
86 |
+
with st.spinner("Reviewing..."):
|
87 |
+
improved_text = generate_review(input_text)
|
88 |
+
highlighted = highlight_changes(input_text, improved_text)
|
89 |
+
|
90 |
+
st.subheader("π§ AI Suggestions:")
|
91 |
+
st.markdown(highlighted)
|
92 |
+
|
93 |
+
if st.button("β
Accept Changes"):
|
94 |
+
st.subheader("βοΈ Final Content:")
|
95 |
+
st.text_area("Updated Blog:", improved_text, height=300)
|
96 |
+
|
97 |
+
if st.button("πͺ Auto-Correct"):
|
98 |
+
st.success("β
AI-corrected content:")
|
99 |
+
st.text_area("Auto-corrected Text:", improved_text, height=300)
|
100 |
+
else:
|
101 |
+
st.warning("β οΈ Please provide input text, image, or URL.")
|