Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,139 +1,201 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
"
|
| 11 |
-
"tiiuae/falcon-7b-instruct",
|
| 12 |
-
"
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
]
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
def chat_with_model(
|
| 18 |
-
user_message,
|
| 19 |
-
history,
|
| 20 |
-
system_message,
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
max_tokens,
|
| 24 |
-
temperature,
|
| 25 |
-
top_p
|
| 26 |
):
|
| 27 |
"""
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
"""
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 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 |
-
#
|
| 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 |
-
|
| 75 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
gr.Markdown(
|
| 77 |
"""
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
""",
|
| 82 |
-
elem_id="title"
|
| 83 |
)
|
| 84 |
-
|
| 85 |
with gr.Row():
|
| 86 |
-
# Left
|
| 87 |
-
with gr.Column(scale=1, min_width=
|
| 88 |
system_message = gr.Textbox(
|
| 89 |
label="System Message",
|
| 90 |
-
value="You are a helpful open-source AI assistant."
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
max_tokens = gr.Slider(
|
|
|
|
| 106 |
minimum=1,
|
| 107 |
maximum=2000,
|
| 108 |
-
step=1,
|
| 109 |
value=512,
|
| 110 |
-
|
| 111 |
)
|
| 112 |
temperature = gr.Slider(
|
| 113 |
-
|
|
|
|
| 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
|
| 128 |
with gr.Column(scale=3):
|
| 129 |
chatbot = gr.ChatInterface(
|
| 130 |
fn=chat_with_model,
|
| 131 |
-
#
|
| 132 |
-
additional_inputs=[
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|