GeminiAi commited on
Commit
c01800b
·
1 Parent(s): 83bc009

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -93
app.py CHANGED
@@ -1,22 +1,15 @@
1
- import gradio as gr
2
- import os
3
  import requests
4
  import json
5
  from huggingface_hub import HfApi
6
  import huggingface_hub
 
7
 
8
  # Constants for Zephyr-7b-Beta model
9
  zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
12
 
13
- # Constants for Gradio UI
14
- constants = """
15
- SYSTEM_PROMPT = "{}"
16
- TITLE = "{}"
17
- EXAMPLE_INPUT = "{}"
18
- """
19
-
20
  # Function to build input prompt for the model
21
  def build_input_prompt(message, chatbot, system_prompt):
22
  input_prompt = "\n" + system_prompt + "</s>\n\n"
@@ -31,7 +24,7 @@ def post_request_beta(payload):
31
  response.raise_for_status()
32
  return response.json()
33
 
34
- # Function to get model predictions
35
  def predict_beta(message, chatbot=[], system_prompt=""):
36
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
37
  data = {"inputs": input_prompt}
@@ -39,100 +32,33 @@ def predict_beta(message, chatbot=[], system_prompt=""):
39
  try:
40
  response_data = post_request_beta(data)
41
  json_obj = response_data[0]
42
-
43
  if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
44
  bot_message = json_obj['generated_text']
45
  return bot_message
46
  elif 'error' in json_obj:
47
- raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
48
  else:
49
  warning_msg = f"Unexpected response: {json_obj}"
50
- raise gr.Error(warning_msg)
51
  except requests.HTTPError as e:
52
  error_msg = f"Request failed with status code {e.response.status_code}"
53
- raise gr.Error(error_msg)
54
  except json.JSONDecodeError as e:
55
  error_msg = f"Failed to decode response as JSON: {str(e)}"
56
- raise gr.Error(error_msg)
57
-
58
- # Function to extract title, system prompt, and example input from model response
59
- def extract_title_prompt_example(text, title, system_prompt, example_input):
60
- try:
61
- text_start = text.rfind("", ) + len("")
62
- text = text[text_start:]
63
- except ValueError:
64
- pass
65
- try:
66
- title_start = text.lower().rfind("title:") + len("title:")
67
- prompt_start = text.lower().rfind("system prompt:")
68
- title = text[title_start:prompt_start].strip()
69
- except ValueError:
70
- pass
71
- try:
72
- prompt_start = text.lower().rfind("system prompt:") + len("system prompt:")
73
- example_start = text.lower().rfind("example input:")
74
- system_prompt = text[prompt_start:example_start].strip()
75
- except ValueError:
76
- pass
77
- try:
78
- example_start = text.lower().rfind("example input:") + len("example input:")
79
- example_input = text[example_start:].strip()
80
- example_input = example_input[:example_input.index("\n")]
81
- except ValueError:
82
- pass
83
- return text, title, system_prompt, example_input
84
-
85
- # Function to make an Open GPT
86
- def make_open_gpt(message, history, current_title, current_system_prompt, current_example_input):
87
- response = predict_beta(message, history, current_system_prompt)
88
- response, title, system_prompt, example_input = extract_title_prompt_example(response, current_title, current_system_prompt, current_example_input)
89
- return "", history + [(message, response)], title, system_prompt, example_input, [(None, welcome_preview_message.format(title, example_input))], example_input, gr.Column(visible=True), gr.Group(visible=True)
90
-
91
- # Function to set title and example input for preview
92
- def set_title_example(title, example):
93
- return [(None, welcome_preview_message.format(title, example))], example, gr.Column(visible=True), gr.Group(visible=True)
94
 
95
- # Function to publish the GPT to Hugging Face Spaces
96
- def publish(textbox_system_prompt, textbox_title, textbox_example, textbox_token):
97
- source_file = 'app_template.py'
98
- destination_file = 'app.py'
99
- constants_formatted = constants.format(textbox_system_prompt, textbox_title, textbox_example)
100
- with open(source_file, 'r') as file:
101
- original_content = file.read()
102
- with open(destination_file, 'w') as file:
103
- file.write(constants_formatted + original_content)
104
- title = strip_invalid_filename_characters(textbox_title, max_bytes=30)
105
- api = HfApi(token=textbox_token)
106
- new_space = api.create_repo(
107
- repo_id=f"open-gpt-{title}",
108
- repo_type="space",
109
- exist_ok=True,
110
- private=False,
111
- space_sdk="gradio",
112
- token=textbox_token,
113
- )
114
- api.upload_file(
115
- repo_id=new_space.repo_id,
116
- path_or_fileobj='app.py',
117
- path_in_repo='app.py',
118
- token=textbox_token,
119
- repo_type="space",
120
- )
121
- api.upload_file(
122
- repo_id=new_space.repo_id,
123
- path_or_fileobj='README_template.md',
124
- path_in_repo='README.md',
125
- token=textbox_token,
126
- repo_type="space",
127
- )
128
- huggingface_hub.add_space_secret(
129
- new_space.repo_id, "HF_TOKEN", textbox_token, token=textbox_token
130
- )
131
 
132
- return gr.Markdown(f"Published to https://huggingface.co/spaces/{new_space.repo_id} ✅", visible=True), gr.Button("Publish", interactive=True)
133
 
134
- # Gradio UI setup
135
- with gr.Blocks(css=css) as demo:
136
- # ... (The rest of your Gradio UI setup)
137
 
138
- demo.launch(share=True)
 
 
 
 
 
1
+ import streamlit as st
 
2
  import requests
3
  import json
4
  from huggingface_hub import HfApi
5
  import huggingface_hub
6
+ import os
7
 
8
  # Constants for Zephyr-7b-Beta model
9
  zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
12
 
 
 
 
 
 
 
 
13
  # Function to build input prompt for the model
14
  def build_input_prompt(message, chatbot, system_prompt):
15
  input_prompt = "\n" + system_prompt + "</s>\n\n"
 
24
  response.raise_for_status()
25
  return response.json()
26
 
27
+ # Function to get the response from the model
28
  def predict_beta(message, chatbot=[], system_prompt=""):
29
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
30
  data = {"inputs": input_prompt}
 
32
  try:
33
  response_data = post_request_beta(data)
34
  json_obj = response_data[0]
35
+
36
  if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
37
  bot_message = json_obj['generated_text']
38
  return bot_message
39
  elif 'error' in json_obj:
40
+ raise st.error(json_obj['error'] + ' Please refresh and try again with a smaller input prompt')
41
  else:
42
  warning_msg = f"Unexpected response: {json_obj}"
43
+ raise st.error(warning_msg)
44
  except requests.HTTPError as e:
45
  error_msg = f"Request failed with status code {e.response.status_code}"
46
+ raise st.error(error_msg)
47
  except json.JSONDecodeError as e:
48
  error_msg = f"Failed to decode response as JSON: {str(e)}"
49
+ raise st.error(error_msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ # Streamlit UI setup
52
+ st.title("GPT Baker with Streamlit")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ # ... (Add your Streamlit UI components)
55
 
56
+ # Example Streamlit components:
57
+ user_input = st.text_input("User Input", "Your default example input")
58
+ submit_button = st.button("Submit")
59
 
60
+ if submit_button:
61
+ # Get the model response
62
+ model_response = predict_beta(user_input)
63
+ st.write("Model Response:")
64
+ st.write(model_response)