Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -6,12 +6,15 @@ import requests
6
  #Streaming endpoint
7
  API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
8
 
 
 
 
9
  #Inferenec function
10
- def predict(openai_gpt4_key, system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
11
 
12
  headers = {
13
  "Content-Type": "application/json",
14
- "Authorization": f"Bearer {openai_gpt4_key}" #Users will provide their own OPENAI_API_KEY
15
  }
16
  print(f"system message is ^^ {system_msg}")
17
  if system_msg.strip() == '':
@@ -102,10 +105,12 @@ def set_visible_false():
102
  def set_visible_true():
103
  return gr.update(visible=True)
104
 
105
- title = """<h1 align="center">🔥GPT4 using Chat-Completions API & 🚀Gradio-Streaming</h1>"""
 
106
  #display message for themes feature
107
- theme_addon_msg = """<center>🌟 This Demo also introduces you to Gradio Themes. Discover more on Gradio website using our <a href="https://gradio.app/theming-guide/" target="_blank">Themeing-Guide🎨</a>! You can develop from scratch, modify an existing Gradio theme, and share your themes with community by uploading them to huggingface-hub easily using <code>theme.push_to_hub()</code>.</center>
108
- """
 
109
 
110
  #Using info to add additional information about System message in GPT4
111
  system_msg_info = """A conversation could begin with a system message to gently instruct the assistant.
@@ -118,18 +123,15 @@ theme = gr.themes.Soft(primary_hue="zinc", secondary_hue="green", neutral_hue="g
118
  with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""",
119
  theme=theme) as demo:
120
  gr.HTML(title)
121
- gr.HTML("""<h3 align="center">🔥This Huggingface Gradio Demo provides you access to GPT4 API with System Messages. Please note that you would be needing an OPENAI API key for GPT4 access🙌</h1>""")
122
  gr.HTML(theme_addon_msg)
123
  gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGPT4?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
124
 
125
  with gr.Column(elem_id = "col_container"):
126
- #Users need to provide their own GPT4 API key, it is no longer provided by Huggingface
127
- with gr.Row():
128
- openai_gpt4_key = gr.Textbox(label="OpenAI GPT4 Key", value="", type="password", placeholder="sk..", info = "You have to provide your own GPT4 keys for this app to function properly",)
129
- with gr.Accordion(label="System message:", open=False):
130
- system_msg = gr.Textbox(label="Instruct the AI Assistant to set its beaviour", info = system_msg_info, value="",placeholder="Type here..")
131
- accordion_msg = gr.HTML(value="🚧 To set System message you will have to refresh the app", visible=False)
132
-
133
  chatbot = gr.Chatbot(label='GPT4', elem_id="chatbot")
134
  inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter")
135
  state = gr.State([])
@@ -146,8 +148,8 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
146
  chat_counter = gr.Number(value=0, visible=False, precision=0)
147
 
148
  #Event handling
149
- inputs.submit( predict, [openai_gpt4_key, system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
150
- b1.click( predict, [openai_gpt4_key, system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
151
 
152
  inputs.submit(set_visible_false, [], [system_msg])
153
  b1.click(set_visible_false, [], [system_msg])
 
6
  #Streaming endpoint
7
  API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
8
 
9
+ #Huggingface provided GPT4 OpenAI API Key
10
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
+
12
  #Inferenec function
13
+ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
14
 
15
  headers = {
16
  "Content-Type": "application/json",
17
+ "Authorization": f"Bearer {OPENAI_API_KEY}"
18
  }
19
  print(f"system message is ^^ {system_msg}")
20
  if system_msg.strip() == '':
 
105
  def set_visible_true():
106
  return gr.update(visible=True)
107
 
108
+ title = """<h1 align="center">🔥GPT4 with ChatCompletions API +🚀Gradio-Streaming</h1>"""
109
+
110
  #display message for themes feature
111
+ theme_addon_msg = """<center>🌟 Discover Gradio Themes with this Demo, featuring v3.22.0! Gradio v3.23.0 also enables seamless Theme sharing. You can develop or modify a theme, and send it to the hub using simple <code>theme.push_to_hub()</code>.
112
+ <br>🏆Participate in Gradio's Theme Building Hackathon to exhibit your creative flair and win fabulous rewards! Join here - <a href="https://huggingface.co/Gradio-Themes" target="_blank">Gradio-Themes-Party🎨</a> 🏆</center>
113
+ """
114
 
115
  #Using info to add additional information about System message in GPT4
116
  system_msg_info = """A conversation could begin with a system message to gently instruct the assistant.
 
123
  with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""",
124
  theme=theme) as demo:
125
  gr.HTML(title)
126
+ gr.HTML("""<h3 align="center">🔥This Huggingface Gradio Demo provides you full access to GPT4 API (4096 token limit). 🎉🥳🎉You don't need any OPENAI API key🙌</h1>""")
127
  gr.HTML(theme_addon_msg)
128
  gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGPT4?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
129
 
130
  with gr.Column(elem_id = "col_container"):
131
+ #GPT4 API Key is provided by Huggingface
132
+ with gr.Accordion(label="System message:", open=False):
133
+ system_msg = gr.Textbox(label="Instruct the AI Assistant to set its beaviour", info = system_msg_info, value="")
134
+ accordion_msg = gr.HTML(value="🚧 To set System message you will have to refresh the app", visible=False)
 
 
 
135
  chatbot = gr.Chatbot(label='GPT4', elem_id="chatbot")
136
  inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter")
137
  state = gr.State([])
 
148
  chat_counter = gr.Number(value=0, visible=False, precision=0)
149
 
150
  #Event handling
151
+ inputs.submit( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
152
+ b1.click( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
153
 
154
  inputs.submit(set_visible_false, [], [system_msg])
155
  b1.click(set_visible_false, [], [system_msg])