TA commited on
Commit
122320e
1 Parent(s): 0b11e5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -29
app.py CHANGED
@@ -1,13 +1,11 @@
1
  import gradio as gr
2
  import os
3
  import requests
 
4
 
5
  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."
6
  TITLE = "Image Prompter"
7
  EXAMPLE_INPUT = "A Reflective cat between stars."
8
- import gradio as gr
9
- import os
10
- import requests
11
 
12
  html_temp = """
13
  <div style="position: absolute; top: 0; right: 0;">
@@ -21,31 +19,21 @@ HF_TOKEN = os.getenv("HF_TOKEN")
21
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
22
 
23
  def build_input_prompt(message, chatbot, system_prompt):
24
- """
25
- Constructs the input prompt string from the chatbot interactions and the current message.
26
- """
27
- input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
28
  for interaction in chatbot:
29
- input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
30
 
31
- input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
32
  return input_prompt
33
 
34
-
35
  def post_request_beta(payload):
36
- """
37
- Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
38
- """
39
  response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
40
- response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
41
  return response.json()
42
 
43
-
44
  def predict_beta(message, chatbot=[], system_prompt=""):
45
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
46
- data = {
47
- "inputs": input_prompt
48
- }
49
 
50
  try:
51
  response_data = post_request_beta(data)
@@ -66,24 +54,19 @@ def predict_beta(message, chatbot=[], system_prompt=""):
66
  error_msg = f"Failed to decode response as JSON: {str(e)}"
67
  raise gr.Error(error_msg)
68
 
69
- def test_preview_chatbot(message, history):
70
  response = predict_beta(message, history, SYSTEM_PROMPT)
71
- text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
72
  response = response[text_start:]
73
  return response
74
 
75
-
76
- welcome_preview_message = f"""
77
  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:
78
 
79
  "{EXAMPLE_INPUT}"
80
  """
81
 
82
- chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
83
- textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
84
-
85
- demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
86
- demo2 = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
87
 
88
- demo.launch(share=True)
89
- demo.launch(share=True)
 
1
  import gradio as gr
2
  import os
3
  import requests
4
+ 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;">
 
19
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
20
 
21
  def build_input_prompt(message, chatbot, system_prompt):
22
+ input_prompt = "\n" + system_prompt + "</s>\n\n"
 
 
 
23
  for interaction in chatbot:
24
+ input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
25
 
26
+ input_prompt = input_prompt + str(message) + "</s>\n"
27
  return input_prompt
28
 
 
29
  def post_request_beta(payload):
 
 
 
30
  response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
31
+ response.raise_for_status()
32
  return response.json()
33
 
 
34
  def predict_beta(message, chatbot=[], system_prompt=""):
35
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
36
+ data = {"inputs": input_prompt}
 
 
37
 
38
  try:
39
  response_data = post_request_beta(data)
 
54
  error_msg = f"Failed to decode response as JSON: {str(e)}"
55
  raise gr.Error(error_msg)
56
 
57
+ def chat_interface(message, history):
58
  response = predict_beta(message, history, SYSTEM_PROMPT)
59
+ text_start = response.rfind("", ) + len("")
60
  response = response[text_start:]
61
  return response
62
 
63
+ welcome_message = f"""
 
64
  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:
65
 
66
  "{EXAMPLE_INPUT}"
67
  """
68
 
69
+ chatbot_setup = gr.Chatbot(layout="panel", value=[(None, welcome_message)])
70
+ textbox_setup = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
 
 
 
71
 
72
+ gr.Interface(fn=chat_interface, inputs=textbox_setup, outputs=chatbot_setup, live=True, share=True).launch()