d221 commited on
Commit
e22dcdd
·
verified ·
1 Parent(s): 2570514

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -92
app.py CHANGED
@@ -1,139 +1,201 @@
1
  import os
2
  import gradio as gr
 
 
3
  from huggingface_hub import InferenceClient
4
 
5
-
6
- AVAILABLE_MODELS = [
7
- "bigscience/bloom-560m", # Smaller, faster version of BLOOM
8
- "bigscience/bloom", # Original 176B parameter Bloom (heavy)
9
- "openlm-research/open_llama_3b", # Smaller 3B LLaMA-like model
10
- "openlm-research/open_llama_7b", # 7B LLaMA-like model
11
- "tiiuae/falcon-7b-instruct", # Falcon 7B instruct
12
- "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5", # OpenAssistant 12B
13
- # Add any other open-source models from HF you like
 
 
 
14
  ]
15
 
16
-
 
 
17
  def chat_with_model(
18
- user_message, # The users message
19
- history, # Chat history (handled automatically by Gradio ChatInterface)
20
- system_message, # The system message/instructions from the left panel
21
- user_api_key, # Optional user-provided HF API key
22
- model_choice, # The model chosen from the dropdown
23
- max_tokens, # Max new tokens
24
- temperature, # Temperature
25
- top_p # Top-p
26
  ):
27
  """
28
- Called every time a user sends a new message.
29
- Uses either the user’s provided HF API key or
30
- does public inference (anonymous) if none is supplied.
 
 
31
  """
32
 
33
- # Decide which key to use
34
- final_api_key = user_api_key.strip() if user_api_key else None # None -> public/no token
35
-
36
- # Initialize InferenceClient with the chosen API key
37
- client = InferenceClient(token=final_api_key)
38
-
39
- # Build the prompt or system instruction
40
- # You can handle chat format in a variety of ways.
41
- # For simplicity, we do a naive approach here:
42
- prompt = (
43
- f"{system_message.strip()}\n\n" # System instructions
44
- f"User: {user_message}\n"
45
- "Assistant:"
46
- )
47
-
48
- # Set generation parameters
49
- generation_params = dict(
50
- temperature=temperature,
51
- max_new_tokens=int(max_tokens),
52
- top_p=top_p,
53
- # Some open-source models do better with a smaller
54
- # repetition_penalty or none at all:
55
- repetition_penalty=1.0,
56
- )
57
 
58
- # Perform streaming text generation
59
  partial_response = ""
60
- stream = client.text_generation(
61
- prompt=prompt,
62
- model=model_choice, # The user's chosen model
63
- stream=True,
64
- details=True,
65
- **generation_params
66
- )
67
- for chunk in stream:
68
- if chunk.token.special:
69
- continue
70
- partial_response += chunk.token.text
71
- yield partial_response
72
-
73
 
74
- with gr.Blocks(theme="soft") as demo:
75
- # Title
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  gr.Markdown(
77
  """
78
- <h1 style="text-align:center; margin-bottom: 5px;">
79
- <b>Open-Source GPT Chatbot</b>
80
- </h1>
81
- """,
82
- elem_id="title"
83
  )
84
-
85
  with gr.Row():
86
- # Left Column: system msg, HF API key, model dropdown, sliders, etc.
87
- with gr.Column(scale=1, min_width=270):
88
  system_message = gr.Textbox(
89
  label="System Message",
90
- value="You are a helpful open-source AI assistant."
 
91
  )
92
 
93
- user_api_key = gr.Textbox(
94
- label="Hugging Face API Key (optional)",
95
- type="password",
96
- placeholder="Leave blank for public/anonymous usage"
 
 
97
  )
98
 
99
- model_choice = gr.Dropdown(
100
- label="Select Open-Source Model",
101
- choices=AVAILABLE_MODELS,
102
- value=AVAILABLE_MODELS[0], # Default to first in list
 
103
  )
104
 
105
  max_tokens = gr.Slider(
 
106
  minimum=1,
107
  maximum=2000,
108
- step=1,
109
  value=512,
110
- label="Max new tokens"
111
  )
112
  temperature = gr.Slider(
113
- minimum=0.1,
 
114
  maximum=2.0,
115
  value=0.7,
116
- step=0.1,
117
- label="Temperature"
118
  )
119
  top_p = gr.Slider(
 
120
  minimum=0.1,
121
  maximum=1.0,
122
  value=0.9,
123
- step=0.01,
124
- label="Top-p (nucleus sampling)"
125
  )
126
 
127
- # Right Column: The chat interface
128
  with gr.Column(scale=3):
129
  chatbot = gr.ChatInterface(
130
  fn=chat_with_model,
131
- # Additional inputs needed by chat_with_model:
132
- additional_inputs=[system_message, user_api_key, model_choice,
133
- max_tokens, temperature, top_p],
134
- type="messages", # Use newer messages format
135
- title="Open-Source Chatbot"
 
 
 
 
 
136
  )
137
 
138
- # Launch the Gradio app
139
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ import openai
4
+ import requests
5
  from huggingface_hub import InferenceClient
6
 
7
+ ###############################################################################
8
+ # 1. List of Models: Some open-source (HF), some require paid API (OpenAI)
9
+ ###############################################################################
10
+ MODEL_OPTIONS = [
11
+ # Open-Source (Hugging Face)
12
+ "Open-Source: bigscience/bloom-560m",
13
+ "Open-Source: tiiuae/falcon-7b-instruct",
14
+ "Open-Source: openlm-research/open_llama_7b",
15
+
16
+ # Paid (OpenAI) - require a valid OPENAI API key
17
+ "OpenAI: gpt-3.5-turbo",
18
+ "OpenAI: gpt-4",
19
  ]
20
 
21
+ ###############################################################################
22
+ # 2. Chat function
23
+ ###############################################################################
24
  def chat_with_model(
25
+ user_message, # user's text input
26
+ history, # chat history (handled by ChatInterface)
27
+ system_message, # system instructions
28
+ chosen_model, # which model from the dropdown
29
+ user_model_api_key, # user-supplied API key for the chosen model
30
+ max_tokens,
31
+ temperature,
32
+ top_p
33
  ):
34
  """
35
+ Depending on the user’s chosen model:
36
+ - If it starts with "Open-Source:", we call Hugging Face InferenceClient
37
+ - If it starts with "OpenAI:", we call the OpenAI ChatCompletion endpoint
38
+ For open-source, the API key can be left blank (anonymous).
39
+ For paid, an API key must be supplied.
40
  """
41
 
42
+ # Standard system text (if user left it empty, we provide a default)
43
+ system_text = system_message.strip() or "You are a helpful AI assistant."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
+ # We'll build partial output as we stream
46
  partial_response = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ ###############################
49
+ # CASE A: OPEN-SOURCE (HF)
50
+ ###############################
51
+ if chosen_model.startswith("Open-Source:"):
52
+ # Extract the actual HF model name
53
+ hf_model = chosen_model.split("Open-Source:")[1].strip()
54
+
55
+ # If the user gave an API key, we use it; otherwise None => anonymous
56
+ hf_token = user_model_api_key.strip() if user_model_api_key else None
57
+ client = InferenceClient(token=hf_token)
58
+
59
+ # Build a naive prompt
60
+ prompt = (
61
+ f"{system_text}\n\n"
62
+ f"User: {user_message}\n"
63
+ "Assistant:"
64
+ )
65
+
66
+ generation_params = dict(
67
+ temperature=temperature,
68
+ max_new_tokens=int(max_tokens),
69
+ top_p=top_p,
70
+ repetition_penalty=1.0
71
+ )
72
+
73
+ try:
74
+ response_stream = client.text_generation(
75
+ prompt=prompt,
76
+ model=hf_model,
77
+ stream=True,
78
+ details=True,
79
+ **generation_params
80
+ )
81
+ for chunk in response_stream:
82
+ if chunk.token.special:
83
+ continue
84
+ partial_response += chunk.token.text
85
+ yield partial_response
86
+
87
+ except Exception as e:
88
+ yield f"Error calling Hugging Face Inference API: {str(e)}"
89
+ return
90
+
91
+ ###############################
92
+ # CASE B: OPENAI
93
+ ###############################
94
+ else:
95
+ # Must have an API key
96
+ if not user_model_api_key.strip():
97
+ yield "Error: This model requires a paid API key. Please provide a valid one."
98
+ return
99
+
100
+ openai.api_key = user_model_api_key.strip()
101
+ openai_model_name = chosen_model.split("OpenAI:")[1].strip() # e.g. "gpt-4"
102
+
103
+ # Build OpenAI chat messages
104
+ messages = [
105
+ {"role": "system", "content": system_text},
106
+ {"role": "user", "content": user_message}
107
+ ]
108
+
109
+ try:
110
+ response = openai.ChatCompletion.create(
111
+ model=openai_model_name,
112
+ messages=messages,
113
+ temperature=temperature,
114
+ max_tokens=int(max_tokens),
115
+ top_p=top_p,
116
+ stream=True
117
+ )
118
+ for chunk in response:
119
+ if "choices" in chunk and len(chunk["choices"]) > 0:
120
+ delta = chunk["choices"][0]["delta"]
121
+ if "content" in delta:
122
+ partial_response += delta["content"]
123
+ yield partial_response
124
+
125
+ except Exception as e:
126
+ yield f"Error calling OpenAI API: {str(e)}"
127
+ return
128
+
129
+ ###############################################################################
130
+ # 3. Build the Gradio Interface
131
+ ###############################################################################
132
+ with gr.Blocks() as demo:
133
  gr.Markdown(
134
  """
135
+ # Multi-Model Chatbot
136
+ Choose from open-source or paid models, and provide an API key if needed.
137
+ """
 
 
138
  )
 
139
  with gr.Row():
140
+ # Left column for parameters
141
+ with gr.Column(scale=1, min_width=300):
142
  system_message = gr.Textbox(
143
  label="System Message",
144
+ value="You are a helpful open-source AI assistant.",
145
+ lines=3,
146
  )
147
 
148
+ # Let user pick model first
149
+ chosen_model = gr.Dropdown(
150
+ label="Select a Model for Your ChatBot",
151
+ choices=MODEL_OPTIONS,
152
+ value=MODEL_OPTIONS[0], # default to the first
153
+ info="Open-Source models can be used anonymously. Paid models require a valid API key."
154
  )
155
 
156
+ # Then the API key tile
157
+ user_model_api_key = gr.Textbox(
158
+ label="API Key for the Chosen Model",
159
+ placeholder="Required if you selected a paid model like GPT-4; optional otherwise",
160
+ type="password"
161
  )
162
 
163
  max_tokens = gr.Slider(
164
+ label="Max Tokens",
165
  minimum=1,
166
  maximum=2000,
 
167
  value=512,
168
+ step=1
169
  )
170
  temperature = gr.Slider(
171
+ label="Temperature",
172
+ minimum=0.0,
173
  maximum=2.0,
174
  value=0.7,
175
+ step=0.1
 
176
  )
177
  top_p = gr.Slider(
178
+ label="Top-p",
179
  minimum=0.1,
180
  maximum=1.0,
181
  value=0.9,
182
+ step=0.01
 
183
  )
184
 
185
+ # Right column for the chat interface
186
  with gr.Column(scale=3):
187
  chatbot = gr.ChatInterface(
188
  fn=chat_with_model,
189
+ # extra inputs
190
+ additional_inputs=[
191
+ system_message,
192
+ chosen_model,
193
+ user_model_api_key,
194
+ max_tokens,
195
+ temperature,
196
+ top_p
197
+ ],
198
+ type="messages"
199
  )
200
 
 
201
  demo.launch()