Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,91 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Set page configuration
|
6 |
+
st.set_page_config(page_title="Gemma Paraphraser", page_icon="✍️")
|
7 |
+
|
8 |
+
# Load model and tokenizer
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model():
|
11 |
+
model_name = "EmTpro01/gemma-paraphraser-16bit"
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
14 |
+
model_name,
|
15 |
+
device_map="cpu",
|
16 |
+
torch_dtype=torch.float16
|
17 |
+
)
|
18 |
+
return model, tokenizer
|
19 |
+
|
20 |
+
# Paraphrase function
|
21 |
+
def paraphrase_text(text, model, tokenizer):
|
22 |
+
# Prepare the prompt using Alpaca format
|
23 |
+
system_prompt = "Below is provided a paragraph, paraphrase it"
|
24 |
+
prompt = f"{system_prompt}\n\n### Input:\n{text}\n\n### Output:\n"
|
25 |
+
|
26 |
+
# Tokenize input
|
27 |
+
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=True)
|
28 |
+
|
29 |
+
# Generate paraphrased text
|
30 |
+
outputs = model.generate(
|
31 |
+
inputs.input_ids,
|
32 |
+
max_length=512, # Adjust based on your needs
|
33 |
+
num_return_sequences=1,
|
34 |
+
temperature=0.7,
|
35 |
+
do_sample=True
|
36 |
+
)
|
37 |
+
|
38 |
+
# Decode and clean the output
|
39 |
+
paraphrased = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
40 |
+
|
41 |
+
# Extract the output part (after "### Output:")
|
42 |
+
output_start = paraphrased.find("### Output:") + len("### Output:")
|
43 |
+
paraphrased_text = paraphrased[output_start:].strip()
|
44 |
+
|
45 |
+
return paraphrased_text
|
46 |
+
|
47 |
+
# Streamlit App
|
48 |
+
def main():
|
49 |
+
st.title("📝 Gemma Paraphraser")
|
50 |
+
st.write("Paraphrase your text using the Gemma model")
|
51 |
+
|
52 |
+
# Load model
|
53 |
+
try:
|
54 |
+
model, tokenizer = load_model()
|
55 |
+
except Exception as e:
|
56 |
+
st.error(f"Error loading model: {e}")
|
57 |
+
return
|
58 |
+
|
59 |
+
# Input text area
|
60 |
+
input_text = st.text_area("Enter text to paraphrase:", height=200)
|
61 |
+
|
62 |
+
# Paraphrase button
|
63 |
+
if st.button("Paraphrase"):
|
64 |
+
if input_text:
|
65 |
+
with st.spinner("Generating paraphrase..."):
|
66 |
+
try:
|
67 |
+
paraphrased_text = paraphrase_text(input_text, model, tokenizer)
|
68 |
+
|
69 |
+
# Display results
|
70 |
+
st.subheader("Paraphrased Text:")
|
71 |
+
st.write(paraphrased_text)
|
72 |
+
|
73 |
+
# Optional: Copy to clipboard
|
74 |
+
st.button("Copy to Clipboard",
|
75 |
+
on_click=lambda: st.write(paraphrased_text))
|
76 |
+
except Exception as e:
|
77 |
+
st.error(f"Error during paraphrasing: {e}")
|
78 |
+
else:
|
79 |
+
st.warning("Please enter some text to paraphrase.")
|
80 |
+
|
81 |
+
# Additional information
|
82 |
+
st.sidebar.info(
|
83 |
+
"Model: EmTpro01/gemma-paraphraser-16bit\n\n"
|
84 |
+
"Tips:\n"
|
85 |
+
"- Enter a paragraph to paraphrase\n"
|
86 |
+
"- Click 'Paraphrase' to generate\n"
|
87 |
+
"- Running on CPU with 16-bit precision"
|
88 |
+
)
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
main()
|