YasirAbdali commited on
Commit
fdbdc93
·
verified ·
1 Parent(s): f4d5088

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -6
app.py CHANGED
@@ -1,10 +1,36 @@
1
- from transformers import pipeline
 
2
  import streamlit as st
3
 
4
- pipe=pipeline("sentiment-analysis")
5
- text=st.text_area("Enter text")
 
 
6
 
7
- if text:
8
- out=pipe(text)
9
 
10
- st.json(out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import streamlit as st
4
 
5
+ # Load GPT-2 model and tokenizer
6
+ model_name = "gpt2"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
 
10
+ # Streamlit app
11
+ st.title("Blog Post Generator")
12
 
13
+ # User input
14
+ prompt = st.text_area("Enter a blog post topic or starting sentence:")
15
+ max_length = st.slider("Maximum length of generated text:", min_value=50, max_value=500, value=200, step=50)
16
+
17
+ if prompt:
18
+ # Tokenize input
19
+ input_ids = tokenizer.encode(prompt, return_tensors="pt")
20
+
21
+ # Generate text
22
+ with torch.no_grad():
23
+ output = model.generate(
24
+ input_ids,
25
+ max_length=max_length,
26
+ num_return_sequences=1,
27
+ no_repeat_ngram_size=2,
28
+ top_k=50,
29
+ top_p=0.95,
30
+ temperature=0.7
31
+ )
32
+
33
+ # Decode and display generated text
34
+ generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
35
+ st.subheader("Generated Blog Post:")
36
+ st.write(generated_text)