Oranblock commited on
Commit
edcf5f0
·
verified ·
1 Parent(s): 88cc4cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -19
app.py CHANGED
@@ -4,11 +4,15 @@ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassifica
4
  from diffusers import StableDiffusionPipeline
5
  import torch
6
  import numpy as np
 
7
  from datasets import Dataset
8
  from huggingface_hub import HfApi
9
  from datetime import datetime, time
10
  from accelerate import Accelerator
11
  from accelerate.utils import set_seed
 
 
 
12
 
13
  # Set a seed for reproducibility
14
  set_seed(42)
@@ -47,8 +51,7 @@ def set_sleep_time():
47
 
48
  @spaces.GPU
49
  @torch.no_grad()
50
- def generate_text():
51
- prompt = "This AI-driven website is unique because"
52
  return text_generator(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
53
 
54
  @spaces.GPU
@@ -66,13 +69,54 @@ def analyze_sentiment(text):
66
  probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
67
  return probabilities.cpu().numpy()[0]
68
 
69
- def generate_content():
70
- text_content = generate_text()
71
- image_prompt = "An abstract representation of a unique website"
72
- image = generate_image(image_prompt)
73
- sentiment = analyze_sentiment(text_content)
 
 
 
 
 
 
 
74
  sentiment_label = "Positive" if sentiment[1] > sentiment[0] else "Negative"
75
- return text_content, image, f"Content Sentiment: {sentiment_label}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  def save_feedback(feedback, rating):
78
  feedback_data.append({"text": feedback, "rating": rating})
@@ -88,25 +132,27 @@ def get_gpu_info():
88
  with gr.Blocks() as demo:
89
  gr.Markdown("# AI-Driven Dynamic Website")
90
  gr.Markdown(get_gpu_info())
 
 
 
 
91
  with gr.Row():
92
- with gr.Column():
93
- text_output = gr.Textbox(label="Generated Content")
94
- image_output = gr.Image(label="Generated Image")
95
- sentiment_output = gr.Textbox(label="Sentiment Analysis")
96
- with gr.Column():
97
- generate_button = gr.Button("Generate New Content")
98
- feedback_input = gr.Textbox(label="Provide Feedback")
99
- feedback_rating = gr.Radio(["Positive", "Negative"], label="Rate the content")
100
- feedback_button = gr.Button("Submit Feedback")
101
- feedback_output = gr.Textbox(label="Feedback Status")
102
 
103
  sleep_button = gr.Button("Set Sleep Time")
104
  sleep_output = gr.Textbox(label="Sleep Time Status")
105
 
106
- generate_button.click(generate_content, outputs=[text_output, image_output, sentiment_output])
107
  feedback_button.click(save_feedback, inputs=[feedback_input, feedback_rating], outputs=feedback_output)
108
  sleep_button.click(set_sleep_time, outputs=sleep_output)
109
 
 
 
 
110
  # Set sleep time when the app starts
111
  set_sleep_time()
112
 
 
4
  from diffusers import StableDiffusionPipeline
5
  import torch
6
  import numpy as np
7
+ import random
8
  from datasets import Dataset
9
  from huggingface_hub import HfApi
10
  from datetime import datetime, time
11
  from accelerate import Accelerator
12
  from accelerate.utils import set_seed
13
+ from PIL import Image
14
+ import io
15
+ import base64
16
 
17
  # Set a seed for reproducibility
18
  set_seed(42)
 
51
 
52
  @spaces.GPU
53
  @torch.no_grad()
54
+ def generate_text(prompt):
 
55
  return text_generator(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
56
 
57
  @spaces.GPU
 
69
  probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
70
  return probabilities.cpu().numpy()[0]
71
 
72
+ def generate_website_content():
73
+ # Generate website title
74
+ title = generate_text("A unique website title:").split(':')[1].strip()
75
+
76
+ # Generate main content
77
+ content = generate_text("A short paragraph about a random topic:").split(':')[1].strip()
78
+
79
+ # Generate an image
80
+ image = generate_image("An abstract representation of " + title)
81
+
82
+ # Analyze sentiment of the content
83
+ sentiment = analyze_sentiment(content)
84
  sentiment_label = "Positive" if sentiment[1] > sentiment[0] else "Negative"
85
+
86
+ # Generate a random color
87
+ color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
88
+
89
+ # Generate a random layout
90
+ layouts = ["centered", "left-aligned", "right-aligned"]
91
+ layout = random.choice(layouts)
92
+
93
+ return title, content, image, sentiment_label, color, layout
94
+
95
+ def update_website():
96
+ title, content, image, sentiment, color, layout = generate_website_content()
97
+
98
+ # Convert PIL Image to base64
99
+ buffered = io.BytesIO()
100
+ image.save(buffered, format="PNG")
101
+ img_str = base64.b64encode(buffered.getvalue()).decode()
102
+
103
+ layout_css = {
104
+ "centered": "text-align: center;",
105
+ "left-aligned": "text-align: left;",
106
+ "right-aligned": "text-align: right;"
107
+ }
108
+
109
+ html_content = f"""
110
+ <div style="font-family: Arial, sans-serif; padding: 20px; background-color: {color}; {layout_css[layout]}">
111
+ <h1>{title}</h1>
112
+ <p>{content}</p>
113
+ <img src="data:image/png;base64,{img_str}" alt="Generated Image" style="max-width: 100%; height: auto;">
114
+ <p>Content Sentiment: {sentiment}</p>
115
+ <button onclick="document.getElementById('refresh_button').click()">Regenerate Website</button>
116
+ </div>
117
+ """
118
+
119
+ return html_content
120
 
121
  def save_feedback(feedback, rating):
122
  feedback_data.append({"text": feedback, "rating": rating})
 
132
  with gr.Blocks() as demo:
133
  gr.Markdown("# AI-Driven Dynamic Website")
134
  gr.Markdown(get_gpu_info())
135
+
136
+ html_output = gr.HTML()
137
+ refresh_button = gr.Button("Regenerate Website", elem_id="refresh_button")
138
+
139
  with gr.Row():
140
+ feedback_input = gr.Textbox(label="Provide Feedback")
141
+ feedback_rating = gr.Radio(["Positive", "Negative"], label="Rate the content")
142
+ feedback_button = gr.Button("Submit Feedback")
143
+
144
+ feedback_output = gr.Textbox(label="Feedback Status")
 
 
 
 
 
145
 
146
  sleep_button = gr.Button("Set Sleep Time")
147
  sleep_output = gr.Textbox(label="Sleep Time Status")
148
 
149
+ refresh_button.click(update_website, outputs=html_output)
150
  feedback_button.click(save_feedback, inputs=[feedback_input, feedback_rating], outputs=feedback_output)
151
  sleep_button.click(set_sleep_time, outputs=sleep_output)
152
 
153
+ # Initialize the website on startup
154
+ demo.load(update_website, outputs=html_output)
155
+
156
  # Set sleep time when the app starts
157
  set_sleep_time()
158