MarwanAshraf22 commited on
Commit
0b3905f
·
1 Parent(s): a8d8832

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -22
app.py CHANGED
@@ -1,25 +1,63 @@
1
- import streamlit as st
2
  import torch
3
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
 
4
 
5
- # Load pre-trained model and tokenizer
6
- model_name = "distilbert-base-uncased"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
-
10
- # Create Streamlit app
11
- st.title("Hugging Face Transformers + Streamlit Example")
12
-
13
- # Define a function to use the model for prediction
14
- @st.cache(allow_output_mutation=True)
15
- def run_model(input_text):
16
- inputs = tokenizer(input_text, return_tensors="pt")
17
- outputs = model(**inputs)
18
- predictions = torch.softmax(outputs.logits, dim=1)
19
- return predictions
20
-
21
- # Streamlit interface
22
- user_input = st.text_input("Enter text:", "Type Here...")
23
- if st.button("Predict"):
24
- predictions = run_model(user_input)
25
- st.write(f"Predictions: {predictions}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
+ import streamlit as st
3
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
4
 
5
+ model_name = "gpt2"
 
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name)
8
+
9
+ generator = pipeline(
10
+ "text-generation",
11
+ model=model,
12
+ tokenizer=tokenizer,
13
+ device=0 if torch.cuda.is_available() else -1
14
+ )
15
+
16
+ # Function for text generation with filtering of repeated sequences
17
+ def generate_text(prompt, section, max_length=200, temperature=0.7, top_k=50, repetition_penalty=1.2):
18
+ return generator(
19
+ f"{section} - {prompt}",
20
+ max_length=max_length,
21
+ do_sample=True,
22
+ top_k=top_k,
23
+ temperature=temperature,
24
+ repetition_penalty=repetition_penalty,
25
+ num_return_sequences=1,
26
+ eos_token_id=tokenizer.eos_token_id,
27
+ )[0]["generated_text"]
28
+
29
+ # Streamlit app
30
+ st.title("AI-Generated Blog Post")
31
+
32
+ # Keyword selection input
33
+ keywords_input = st.text_input("Step 1: Keyword Selection (Separate keywords with commas)","Artificial Intelligence")
34
+ keywords = [word.strip() for word in keywords_input.split(',')]
35
+
36
+ # Display generated content on button click
37
+ if st.button('Generate Article'):
38
+ if keywords_input:
39
+
40
+ generated_text = " ".join(keywords)
41
+ intro_text = generate_text(generated_text, "Introduction", max_length=200, temperature=0.7, top_k=50)
42
+ body_text = generate_text(generated_text, "Body", max_length=500, temperature=0.7, top_k=50)
43
+ conclusion_text = generate_text(generated_text, "Conclusion", max_length=150, temperature=0.7, top_k=50)
44
+
45
+ # Displaying the sections with adjusted parameters
46
+ st.header("Introduction")
47
+ st.write(intro_text)
48
+
49
+ st.header("Body")
50
+ st.write(body_text)
51
+
52
+ st.header("Conclusion")
53
+ st.write(conclusion_text)
54
+ else:
55
+ st.warning("Please input keywords to generate content.")
56
+
57
+ # Sidebar with instructions
58
+ st.sidebar.title("Instructions")
59
+ st.sidebar.write(
60
+ "1. Enter keywords related to the topic you want to generate content about."
61
+ "\n2. Click 'Generate Article' to create the AI-generated blog post."
62
+ "\n3. Explore the Introduction, Body, and Conclusion sections of the generated content."
63
+ )