TA commited on
Commit
831bb79
1 Parent(s): b768e87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -18
app.py CHANGED
@@ -1,6 +1,7 @@
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"
@@ -10,24 +11,16 @@ HF_TOKEN = os.getenv("HF_TOKEN")
10
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
11
 
12
  def build_input_prompt(message, chatbot, system_prompt):
13
- """
14
- Constructs the input prompt string from the chatbot interactions and the current message.
15
- """
16
  input_prompt = system_prompt + "\n\n" + message
17
  return input_prompt
18
 
19
-
20
  def post_request(model_url, payload):
21
- """
22
- Sends a POST request to the specified model URL and returns the JSON response.
23
- """
24
  response = requests.post(model_url, headers=HEADERS, json=payload)
25
- response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
26
  return response.json()
27
 
28
-
29
- def predict(model_url, message, chatbot=[], system_prompt=""):
30
- input_prompt = build_input_prompt(message, chatbot, system_prompt)
31
  data = {
32
  "prompt": input_prompt,
33
  "max_new_tokens": 256,
@@ -41,18 +34,16 @@ def predict(model_url, message, chatbot=[], system_prompt=""):
41
  return bot_message
42
  except requests.HTTPError as e:
43
  error_msg = f"Request failed with status code {e.response.status_code}"
44
- raise gr.Error(error_msg)
45
  except json.JSONDecodeError as e:
46
  error_msg = f"Failed to decode response as JSON: {str(e)}"
47
- raise gr.Error(error_msg)
48
 
49
-
50
- def test_preview_chatbot(message, history):
51
  model_url = "https://huggingface.co/chat/models/llama/llama-3b"
52
- response = predict(model_url, message, history, SYSTEM_PROMPT)
53
  return response
54
 
55
-
56
  welcome_preview_message = f"""
57
  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:
58
  "{EXAMPLE_INPUT}"
@@ -63,7 +54,7 @@ textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
63
 
64
  demo = gr.Interface(
65
  fn=test_preview_chatbot,
66
- inputs=["text", "state"],
67
  outputs="text",
68
  title=TITLE,
69
  description="Image Prompter"
 
1
  import gradio as gr
2
  import os
3
  import requests
4
+ from gradio import Error
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"
 
11
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
12
 
13
  def build_input_prompt(message, chatbot, system_prompt):
 
 
 
14
  input_prompt = system_prompt + "\n\n" + message
15
  return input_prompt
16
 
 
17
  def post_request(model_url, payload):
 
 
 
18
  response = requests.post(model_url, headers=HEADERS, json=payload)
19
+ response.raise_for_status()
20
  return response.json()
21
 
22
+ def predict(model_url, message, system_prompt):
23
+ input_prompt = build_input_prompt(message, [], system_prompt)
 
24
  data = {
25
  "prompt": input_prompt,
26
  "max_new_tokens": 256,
 
34
  return bot_message
35
  except requests.HTTPError as e:
36
  error_msg = f"Request failed with status code {e.response.status_code}"
37
+ raise Error(error_msg)
38
  except json.JSONDecodeError as e:
39
  error_msg = f"Failed to decode response as JSON: {str(e)}"
40
+ raise Error(error_msg)
41
 
42
+ def test_preview_chatbot(message):
 
43
  model_url = "https://huggingface.co/chat/models/llama/llama-3b"
44
+ response = predict(model_url, message, SYSTEM_PROMPT)
45
  return response
46
 
 
47
  welcome_preview_message = f"""
48
  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:
49
  "{EXAMPLE_INPUT}"
 
54
 
55
  demo = gr.Interface(
56
  fn=test_preview_chatbot,
57
+ inputs="text",
58
  outputs="text",
59
  title=TITLE,
60
  description="Image Prompter"