Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load the language model pipeline
|
5 |
+
# You can replace "gpt2" with another model or a locally fine-tuned model as desired
|
6 |
+
@st.cache_resource
|
7 |
+
def load_model():
|
8 |
+
return pipeline("text-generation", model="gpt2")
|
9 |
+
|
10 |
+
llm = load_model()
|
11 |
+
|
12 |
+
# Set up Streamlit columns for layout
|
13 |
+
col1, col2 = st.columns(2)
|
14 |
+
|
15 |
+
with col1:
|
16 |
+
# User input box for text input
|
17 |
+
user_input = st.text_input("Enter your text:", "")
|
18 |
+
|
19 |
+
# Static backend text to combine with user input
|
20 |
+
backend_text = "Predefined text: "
|
21 |
+
combined_text = backend_text + user_input
|
22 |
+
|
23 |
+
# Button to trigger LLM generation
|
24 |
+
if st.button("Generate"):
|
25 |
+
if user_input.strip(): # Ensure input is not empty
|
26 |
+
with st.spinner("Generating response..."):
|
27 |
+
# Generate response from the LLM with some constraints
|
28 |
+
response = llm(combined_text, max_length=100, num_return_sequences=1)
|
29 |
+
# Extract generated text from LLM output
|
30 |
+
output_text = response[0]['generated_text']
|
31 |
+
else:
|
32 |
+
output_text = "Please provide some input text."
|
33 |
+
|
34 |
+
with col2:
|
35 |
+
# Display the output in a text area
|
36 |
+
st.text_area("Output:", output_text, height=200, key="output_text")
|
37 |
+
|
38 |
+
# Copy button (uses Streamlit Components to trigger copying)
|
39 |
+
copy_script = """
|
40 |
+
<script>
|
41 |
+
function copyToClipboard(text) {
|
42 |
+
navigator.clipboard.writeText(text).then(
|
43 |
+
function() { console.log('Copying to clipboard succeeded'); },
|
44 |
+
function(err) { console.error('Could not copy text: ', err); }
|
45 |
+
);
|
46 |
+
}
|
47 |
+
</script>
|
48 |
+
"""
|
49 |
+
# Add the script to the page
|
50 |
+
st.markdown(copy_script, unsafe_allow_html=True)
|
51 |
+
|
52 |
+
# Button to copy the output text
|
53 |
+
if st.button("Copy Output"):
|
54 |
+
# Display the output text in a way accessible to JS
|
55 |
+
st.write(f'<button onclick="copyToClipboard(document.getElementById(\'output_text\').value)">Copy</button>', unsafe_allow_html=True)
|
56 |
+
|