File size: 2,809 Bytes
0bccd65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaa165b
0bccd65
 
 
 
1
2
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import streamlit as st
from transformers import pipeline
import torch

# Set page title and layout
st.set_page_config(page_title="Paraphrase Chat Interface", layout="wide")

# Initialize the model
@st.cache_resource
def load_model():
    model = "GeneZC/MiniChat-2-3B"
    return pipeline(task='text-generation', model=model)

generator = load_model()

tones = {
    'natural': 'human, authentic',
    'fluency': 'readable, clarified',
    'formal': 'sophisticated',
    'academic': 'technical and scholarly',
    'simple': 'simple and easily understandable',
}

def generate(text, max_length):
    return generator(text, max_length=max_length, num_return_sequences=1)

def respond(message, tone="natural", max_length=512):
    prompt = f"<s> [|User|]Paraphrase this text in a more {tones[tone]} way: {message} </s>[|Assistant|]"
    text = generate(prompt, max_length)
    text = text[0]["generated_text"]
    text = text.split("[|Assistant|]", 1)[1]
    return text

# Streamlit UI
st.title("Paraphrase Chat Interface")

# Sidebar for tone and max length selection
st.sidebar.header("Settings")
tone = st.sidebar.selectbox("Select Tone", list(tones.keys()), index=0)
max_length = st.sidebar.slider("Max new tokens", min_value=1, max_value=2048, value=512, step=1)

# Explanation of the app
st.sidebar.markdown("""
## How to use
1. Type your text in the chat input below.
2. Select a tone from the dropdown menu.
3. Adjust the max token length if needed.
4. Press Enter to get a paraphrased version.

The AI will rephrase your text in the selected tone.
""")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# React to user input
if prompt := st.chat_input("What would you like to paraphrase?"):
    # Display user message in chat message container
    st.chat_message("user").markdown(prompt)
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})

    # Show a spinner while processing
    with st.spinner("Generating paraphrase..."):
        response = respond(prompt, tone, max_length)
    
    # Display assistant response in chat message container
    with st.chat_message("assistant"):
        st.markdown(response)
    # Add assistant response to chat history
    st.session_state.messages.append({"role": "assistant", "content": response})

# Add a button to clear chat history
if st.button("Clear Chat History"):
    st.session_state.messages = []
    st.rerun()  # Use st.rerun() instead of st.experimental_rerun() 

# Footer
st.markdown("---")
st.markdown("Powered by Hugging Face Transformers and Streamlit")