Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Set page title and layout
|
6 |
+
st.set_page_config(page_title="Paraphrase Chat Interface", layout="wide")
|
7 |
+
|
8 |
+
# Initialize the model
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model():
|
11 |
+
model = "GeneZC/MiniChat-2-3B"
|
12 |
+
return pipeline(task='text-generation', model=model)
|
13 |
+
|
14 |
+
generator = load_model()
|
15 |
+
|
16 |
+
tones = {
|
17 |
+
'natural': 'human, authentic',
|
18 |
+
'fluency': 'readable, clarified',
|
19 |
+
'formal': 'sophisticated',
|
20 |
+
'academic': 'technical and scholarly',
|
21 |
+
'simple': 'simple and easily understandable',
|
22 |
+
}
|
23 |
+
|
24 |
+
def generate(text, max_length):
|
25 |
+
return generator(text, max_length=max_length, num_return_sequences=1)
|
26 |
+
|
27 |
+
def respond(message, tone="natural", max_length=512):
|
28 |
+
prompt = f"<s> [|User|]Paraphrase this text in a more {tones[tone]} way: {message} </s>[|Assistant|]"
|
29 |
+
text = generate(prompt, max_length)
|
30 |
+
text = text[0]["generated_text"]
|
31 |
+
text = text.split("[|Assistant|]", 1)[1]
|
32 |
+
return text
|
33 |
+
|
34 |
+
# Streamlit UI
|
35 |
+
st.title("Paraphrase Chat Interface")
|
36 |
+
|
37 |
+
# Sidebar for tone and max length selection
|
38 |
+
st.sidebar.header("Settings")
|
39 |
+
tone = st.sidebar.selectbox("Select Tone", list(tones.keys()), index=0)
|
40 |
+
max_length = st.sidebar.slider("Max new tokens", min_value=1, max_value=2048, value=512, step=1)
|
41 |
+
|
42 |
+
# Explanation of the app
|
43 |
+
st.sidebar.markdown("""
|
44 |
+
## How to use
|
45 |
+
1. Type your text in the chat input below.
|
46 |
+
2. Select a tone from the dropdown menu.
|
47 |
+
3. Adjust the max token length if needed.
|
48 |
+
4. Press Enter to get a paraphrased version.
|
49 |
+
|
50 |
+
The AI will rephrase your text in the selected tone.
|
51 |
+
""")
|
52 |
+
|
53 |
+
# Initialize chat history
|
54 |
+
if "messages" not in st.session_state:
|
55 |
+
st.session_state.messages = []
|
56 |
+
|
57 |
+
# Display chat messages from history on app rerun
|
58 |
+
for message in st.session_state.messages:
|
59 |
+
with st.chat_message(message["role"]):
|
60 |
+
st.markdown(message["content"])
|
61 |
+
|
62 |
+
# React to user input
|
63 |
+
if prompt := st.chat_input("What would you like to paraphrase?"):
|
64 |
+
# Display user message in chat message container
|
65 |
+
st.chat_message("user").markdown(prompt)
|
66 |
+
# Add user message to chat history
|
67 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
68 |
+
|
69 |
+
# Show a spinner while processing
|
70 |
+
with st.spinner("Generating paraphrase..."):
|
71 |
+
response = respond(prompt, tone, max_length)
|
72 |
+
|
73 |
+
# Display assistant response in chat message container
|
74 |
+
with st.chat_message("assistant"):
|
75 |
+
st.markdown(response)
|
76 |
+
# Add assistant response to chat history
|
77 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
78 |
+
|
79 |
+
# Add a button to clear chat history
|
80 |
+
if st.button("Clear Chat History"):
|
81 |
+
st.session_state.messages = []
|
82 |
+
st.experimental_rerun()
|
83 |
+
|
84 |
+
# Footer
|
85 |
+
st.markdown("---")
|
86 |
+
st.markdown("Powered by Hugging Face Transformers and Streamlit")
|