NerdN commited on
Commit
316b18c
1 Parent(s): ed40985

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -25
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import os
3
  import requests
@@ -5,14 +6,31 @@ import json
5
 
6
  SYSTEM_PROMPT = "As an LLM, your job is to generate detailed prompts that start with generate the image, for image generation models based on user input. Be descriptive and specific, but also make sure your prompts are clear and concise."
7
  TITLE = "Image Prompter"
8
- EXAMPLE_INPUT = "A Reflective cat between stars."
 
 
 
 
 
9
 
10
  html_temp = """
11
- <div style="position: absolute; top: 0; right: 0;">
12
- <img src='https://www.bing.com/images/create/a-black-cat-with-a-shiny2c-reflective-coat-is-float/1-656c50e048424f578a489a4875acd14f?id=%2b0DNSc2C8Sw26e32dIzHZA%3d%3d&view=detailv2&idpp=genimg&idpclose=1&FORM=SYDBIC' alt='Your Image' style='width:100px;height:100px;'>
13
- </div>
14
- <div style="position: absolute; bottom: 0; left: 0; padding: 10px;">
15
- <p>{EXAMPLE_INPUT}</p>
 
 
 
 
 
 
 
 
 
 
 
 
16
  </div>
17
  """
18
 
@@ -22,36 +40,23 @@ HF_TOKEN = os.getenv("HF_TOKEN")
22
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
23
 
24
  def build_input_prompt(message, chatbot, system_prompt):
25
- """
26
- Constructs the input prompt string from the chatbot interactions and the current message.
27
- """
28
  input_prompt = "\n" + system_prompt + "</s>\n\n"
29
  for interaction in chatbot:
30
  input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
31
-
32
  input_prompt = input_prompt + str(message) + "</s>\n"
33
  return input_prompt
34
 
35
-
36
  def post_request_beta(payload):
37
- """
38
- Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
39
- """
40
  response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
41
- response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
42
  return response.json()
43
 
44
-
45
  def predict_beta(message, chatbot=[], system_prompt=""):
46
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
47
- data = {
48
- "inputs": input_prompt
49
- }
50
-
51
  try:
52
  response_data = post_request_beta(data)
53
  json_obj = response_data[0]
54
-
55
  if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
56
  bot_message = json_obj['generated_text']
57
  return bot_message
@@ -72,13 +77,17 @@ def test_preview_chatbot(message, history):
72
  return response
73
 
74
  welcome_preview_message = f"""
75
- Expand your imagination and broaden your horizons with LLM. Welcome to **{TITLE}**!:\nThis is a chatbot that can generate detailed prompts for image generation models based on simple and short user input.\nSay something like:
76
 
77
- "{EXAMPLE_INPUT}"
 
 
 
78
  """
79
 
80
  chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
81
- textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
 
 
82
 
83
- demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview, examples=[[EXAMPLE_INPUT]])
84
  demo.launch(share=True)
 
1
+
2
  import gradio as gr
3
  import os
4
  import requests
 
6
 
7
  SYSTEM_PROMPT = "As an LLM, your job is to generate detailed prompts that start with generate the image, for image generation models based on user input. Be descriptive and specific, but also make sure your prompts are clear and concise."
8
  TITLE = "Image Prompter"
9
+ EXAMPLE_INPUTS = [
10
+ {"prompt": "A Reflective cat between stars.", "image_url": "https://www.bing.com/images/create/a-black-cat-with-a-shiny2c-reflective-coat-is-float/1-656c50e048424f578a489a4875acd14f?id=%2b0DNSc2C8Sw26e32dIzHZA%3d%3d&view=detailv2&idpp=genimg&idpclose=1&FORM=SYDBIC"},
11
+ {"prompt": "A Stunning sunset over the mountains.", "image_url": "https://www.example.com/sunset_image.jpg"},
12
+ {"prompt": "An Enchanted forest with fireflies.", "image_url": "https://www.example.com/forest_image.jpg"},
13
+ {"prompt": "A Mysterious spaceship in the night sky.", "image_url": "https://www.example.com/spaceship_image.jpg"}
14
+ ]
15
 
16
  html_temp = """
17
+ <div style="display: flex; justify-content: space-between; padding: 10px;">
18
+ <div>
19
+ <img src='{image_url_1}' alt='Image 1' style='width:100px;height:100px;'>
20
+ <p>{prompt_1}</p>
21
+ </div>
22
+ <div>
23
+ <img src='{image_url_2}' alt='Image 2' style='width:100px;height:100px;'>
24
+ <p>{prompt_2}</p>
25
+ </div>
26
+ <div>
27
+ <img src='{image_url_3}' alt='Image 3' style='width:100px;height:100px;'>
28
+ <p>{prompt_3}</p>
29
+ </div>
30
+ <div>
31
+ <img src='{image_url_4}' alt='Image 4' style='width:100px;height:100px;'>
32
+ <p>{prompt_4}</p>
33
+ </div>
34
  </div>
35
  """
36
 
 
40
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
41
 
42
  def build_input_prompt(message, chatbot, system_prompt):
 
 
 
43
  input_prompt = "\n" + system_prompt + "</s>\n\n"
44
  for interaction in chatbot:
45
  input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
 
46
  input_prompt = input_prompt + str(message) + "</s>\n"
47
  return input_prompt
48
 
 
49
  def post_request_beta(payload):
 
 
 
50
  response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
51
+ response.raise_for_status()
52
  return response.json()
53
 
 
54
  def predict_beta(message, chatbot=[], system_prompt=""):
55
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
56
+ data = {"inputs": input_prompt}
 
 
 
57
  try:
58
  response_data = post_request_beta(data)
59
  json_obj = response_data[0]
 
60
  if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
61
  bot_message = json_obj['generated_text']
62
  return bot_message
 
77
  return response
78
 
79
  welcome_preview_message = f"""
80
+ Expand your imagination and broaden your horizons with LLM. Welcome to **{TITLE}**!:\nThis is a chatbot that can generate detailed prompts for image generation models based on simple and short user input.\nTry one of these prompts:
81
 
82
+ - "{EXAMPLE_INPUTS[0]['prompt']}"
83
+ - "{EXAMPLE_INPUTS[1]['prompt']}"
84
+ - "{EXAMPLE_INPUTS[2]['prompt']}"
85
+ - "{EXAMPLE_INPUTS[3]['prompt']}"
86
  """
87
 
88
  chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
89
+ textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUTS[0]['prompt'])
90
+
91
+ demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview, examples=[[EXAMPLE_INPUTS[0]['prompt']]])
92
 
 
93
  demo.launch(share=True)