Erfan11 commited on
Commit
c582cf1
1 Parent(s): 25e4f35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -1,17 +1,29 @@
1
- import gradio as gr
 
2
 
3
- def respond_to_input(user_input):
4
- return f"You said: {user_input}"
 
 
5
 
6
- # Define the Gradio interface
7
- with gr.Blocks() as demo:
8
- # Create a row layout with input and output textboxes
9
- with gr.Row():
10
- input_textbox = gr.Textbox(label="Input", placeholder="Type something...", lines=1)
11
- output_textbox = gr.Textbox(label="Output", interactive=False, lines=1)
 
 
 
 
12
 
13
- # Link the input textbox to the output textbox
14
- input_textbox.submit(respond_to_input, inputs=input_textbox, outputs=output_textbox)
15
 
16
- # Launch the Gradio interface
17
- demo.launch()
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
3
 
4
+ # Load pre-trained model and tokenizer
5
+ model_name = "gpt2"
6
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
7
+ model = GPT2LMHeadModel.from_pretrained(model_name)
8
 
9
+ def generate_text(prompt, max_length=50):
10
+ # Encode the input prompt
11
+ inputs = tokenizer.encode(prompt, return_tensors="pt")
12
+
13
+ # Generate text
14
+ outputs = model.generate(inputs, max_length=max_length, num_return_sequences=1)
15
+
16
+ # Decode the generated text
17
+ text = tokenizer.decode(outputs[0], skip_special_tokens=True)
18
+ return text
19
 
20
+ # Streamlit app
21
+ st.title("GPT-2 Text Generator")
22
 
23
+ prompt = st.text_area("Input", "Once upon a time...")
24
+ max_length = st.slider("Max Length", min_value=10, max_value=100, value=50)
25
+
26
+ if st.button("Generate"):
27
+ generated_text = generate_text(prompt, max_length)
28
+ st.subheader("Generated Text")
29
+ st.write(generated_text)