Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,185 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="stepfun-ai/Step-3.5-Flash-Base")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
with gr.Blocks() as demo:
|
| 63 |
-
with gr.Sidebar():
|
| 64 |
-
gr.LoginButton()
|
| 65 |
-
chatbot.render()
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import random
|
| 4 |
|
| 5 |
+
# 50 лучших моделей для выбора
|
| 6 |
+
MODELS = {
|
| 7 |
+
"🚀 LLaMA-3-70B": "meta-llama/Meta-Llama-3-70B-Instruct",
|
| 8 |
+
"⚡ LLaMA-3-8B": "meta-llama/Meta-Llama-3-8B-Instruct",
|
| 9 |
+
"🤖 Mistral-7B": "mistralai/Mistral-7B-Instruct-v0.3",
|
| 10 |
+
"🌪️ Mixtral-8x7B": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 11 |
+
"💬 Mixtral-8x22B": "mistralai/Mixtral-8x22B-Instruct-v0.1",
|
| 12 |
+
"🧠 Qwen-2.5-72B": "Qwen/Qwen2.5-72B-Instruct",
|
| 13 |
+
"📚 Qwen-2.5-7B": "Qwen/Qwen2.5-7B-Instruct",
|
| 14 |
+
"⚙️ Gemma-2-27B": "google/gemma-2-27b-it",
|
| 15 |
+
"🔧 Gemma-2-9B": "google/gemma-2-9b-it",
|
| 16 |
+
"🎯 Gemma-2B": "google/gemma-2b-it",
|
| 17 |
+
"🌟 Command-R+": "CohereForAI/c4ai-command-r-plus",
|
| 18 |
+
"📝 Command-R": "CohereForAI/c4ai-command-r-v01",
|
| 19 |
+
"🔥 DeepSeek-V3": "deepseek-ai/DeepSeek-V3",
|
| 20 |
+
"⚡ DeepSeek-R1": "deepseek-ai/DeepSeek-R1",
|
| 21 |
+
"🎨 DeepSeek-Coder": "deepseek-ai/DeepSeek-Coder-33B-instruct",
|
| 22 |
+
"💡 Phi-3-mini": "microsoft/Phi-3-mini-4k-instruct",
|
| 23 |
+
"📖 Phi-3-small": "microsoft/Phi-3-small-8k-instruct",
|
| 24 |
+
"🏆 Phi-3-medium": "microsoft/Phi-3-medium-4k-instruct",
|
| 25 |
+
"🚅 Starling-LM": "berkeley-nest/Starling-LM-7B-alpha",
|
| 26 |
+
"✨ Neural-7B": "Intel/neural-chat-7b-v3-3",
|
| 27 |
+
"🔮 SOLAR-10.7B": "upstage/SOLAR-10.7B-Instruct-v1.0",
|
| 28 |
+
"🌊 Yi-34B": "01-ai/Yi-34B-Chat",
|
| 29 |
+
"🌿 Yi-6B": "01-ai/Yi-6B-Chat",
|
| 30 |
+
"💫 Zephyr-7B": "HuggingFaceH4/zephyr-7b-beta",
|
| 31 |
+
"🔥 Tulu-2-70B": "allenai/tulu-2-70b",
|
| 32 |
+
"⚡ Tulu-2-13B": "allenai/tulu-2-13b",
|
| 33 |
+
"🎯 OpenHermes-2.5": "teknium/OpenHermes-2.5-Mistral-7B",
|
| 34 |
+
"🚀 Nous-Hermes-2": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
|
| 35 |
+
"💬 Vicuna-33B": "lmsys/vicuna-33b-v1.3",
|
| 36 |
+
"🧪 WizardLM-70B": "WizardLM/WizardLM-70B-V1.0",
|
| 37 |
+
"📊 WizardCoder-15B": "WizardLM/WizardCoder-15B-V1.0",
|
| 38 |
+
"🎓 WizardMath-70B": "WizardLM/WizardMath-70B-V1.0",
|
| 39 |
+
"🔬 Falcon-40B": "tiiuae/falcon-40b-instruct",
|
| 40 |
+
"🌍 Falcon-7B": "tiiuae/falcon-7b-instruct",
|
| 41 |
+
"⚡ MPT-30B": "mosaicml/mpt-30b-chat",
|
| 42 |
+
"🔥 MPT-7B": "mosaicml/mpt-7b-chat",
|
| 43 |
+
"💫 Dolly-v2-12B": "databricks/dolly-v2-12b",
|
| 44 |
+
"✨ Dolly-v2-3B": "databricks/dolly-v2-3b",
|
| 45 |
+
"🌟 Pythia-12B": "EleutherAI/pythia-12b-deduped",
|
| 46 |
+
"🚀 RedPajama-3B": "togethercomputer/RedPajama-INCITE-Chat-3B-v1",
|
| 47 |
+
"🎯 Guanaco-65B": "timdettmers/guanaco-65b",
|
| 48 |
+
"📚 Guanaco-33B": "timdettmers/guanaco-33b",
|
| 49 |
+
"🧠 Guanaco-13B": "timdettmers/guanaco-13b",
|
| 50 |
+
"⚡ Guanaco-7B": "timdettmers/guanaco-7b",
|
| 51 |
+
"💡 Cerebras-GPT-13B": "cerebras/Cerebras-GPT-13B",
|
| 52 |
+
"🔧 GPT-NeoX-20B": "EleutherAI/gpt-neox-20b",
|
| 53 |
+
"🌊 GPT-J-6B": "EleutherAI/gpt-j-6b",
|
| 54 |
+
"🎨 BLIP-2": "Salesforce/blip2-opt-2.7b",
|
| 55 |
+
"📝 FLAN-T5-XXL": "google/flan-t5-xxl",
|
| 56 |
+
"⚙️ FLAN-UL2": "google/flan-ul2"
|
| 57 |
+
}
|
| 58 |
|
| 59 |
+
def format_model_name(name):
|
| 60 |
+
return f"{name[:30]}..." if len(name) > 30 else name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
def respond(message, history, model, system_message, max_tokens, temperature, top_p, hf_token):
|
| 63 |
+
try:
|
| 64 |
+
client = InferenceClient(token=hf_token.token if hf_token else None, model=MODELS[model])
|
| 65 |
+
|
| 66 |
+
messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": message}]
|
| 67 |
+
|
| 68 |
+
response = ""
|
| 69 |
+
for chunk in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
|
| 70 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 71 |
+
response += chunk.choices[0].delta.content
|
| 72 |
+
yield response
|
| 73 |
+
except Exception as e:
|
| 74 |
+
yield f"❌ Error: {str(e)}"
|
| 75 |
|
| 76 |
+
# Крутой CSS дизайн
|
| 77 |
+
css = """
|
| 78 |
+
:root {
|
| 79 |
+
--primary: #8b5cf6;
|
| 80 |
+
--secondary: #ec4899;
|
| 81 |
+
--dark: #1f2937;
|
| 82 |
+
}
|
| 83 |
+
.gradio-container {
|
| 84 |
+
max-width: 1200px !important;
|
| 85 |
+
margin: auto !important;
|
| 86 |
+
font-family: 'Inter', sans-serif !important;
|
| 87 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 88 |
+
min-height: 100vh;
|
| 89 |
+
}
|
| 90 |
+
.chat-message {
|
| 91 |
+
border-radius: 20px !important;
|
| 92 |
+
border: none !important;
|
| 93 |
+
box-shadow: 0 10px 40px rgba(0,0,0,0.1) !important;
|
| 94 |
+
transition: all 0.3s ease !important;
|
| 95 |
+
}
|
| 96 |
+
.chat-message:hover {
|
| 97 |
+
transform: translateY(-2px);
|
| 98 |
+
box-shadow: 0 15px 50px rgba(0,0,0,0.15) !important;
|
| 99 |
+
}
|
| 100 |
+
.user-message {
|
| 101 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 102 |
+
color: white !important;
|
| 103 |
+
}
|
| 104 |
+
.bot-message {
|
| 105 |
+
background: white !important;
|
| 106 |
+
color: #333 !important;
|
| 107 |
+
}
|
| 108 |
+
button.primary {
|
| 109 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 110 |
+
border: none !important;
|
| 111 |
+
border-radius: 12px !important;
|
| 112 |
+
padding: 10px 25px !important;
|
| 113 |
+
font-weight: 600 !important;
|
| 114 |
+
transition: all 0.3s ease !important;
|
| 115 |
+
}
|
| 116 |
+
button.primary:hover {
|
| 117 |
+
transform: scale(1.05);
|
| 118 |
+
box-shadow: 0 10px 30px rgba(102,126,234,0.4) !important;
|
| 119 |
+
}
|
| 120 |
+
.sidebar {
|
| 121 |
+
background: rgba(255,255,255,0.95) !important;
|
| 122 |
+
border-radius: 20px !important;
|
| 123 |
+
padding: 20px !important;
|
| 124 |
+
backdrop-filter: blur(10px);
|
| 125 |
+
}
|
| 126 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 129 |
+
with gr.Sidebar(open=True):
|
| 130 |
+
gr.Markdown("""
|
| 131 |
+
# 🤖 AI Chat Hub
|
| 132 |
+
### 50+ Models • Instant Answers
|
| 133 |
+
""")
|
| 134 |
+
|
| 135 |
+
login_btn = gr.LoginButton()
|
| 136 |
+
|
| 137 |
+
model_dropdown = gr.Dropdown(
|
| 138 |
+
choices=list(MODELS.keys()),
|
| 139 |
+
value="🚀 LLaMA-3-70B",
|
| 140 |
+
label="🎯 Select Model",
|
| 141 |
+
interactive=True
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 145 |
+
system_msg = gr.Textbox(
|
| 146 |
+
value="You are a helpful AI assistant.",
|
| 147 |
+
label="System Prompt",
|
| 148 |
+
lines=2
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
with gr.Row():
|
| 152 |
+
max_tokens = gr.Slider(100, 4096, 1024, step=100, label="Max Tokens")
|
| 153 |
+
temperature = gr.Slider(0.1, 2.0, 0.7, step=0.1, label="Temperature")
|
| 154 |
+
top_p = gr.Slider(0.1, 1.0, 0.95, step=0.05, label="Top P")
|
| 155 |
+
|
| 156 |
+
gr.Markdown("""
|
| 157 |
+
💡 **Tips:**
|
| 158 |
+
- **Higher temperature** = more creative
|
| 159 |
+
- **Lower temperature** = more focused
|
| 160 |
+
- **Top P** controls diversity
|
| 161 |
+
""")
|
| 162 |
+
|
| 163 |
+
# Главный чат
|
| 164 |
+
chatbot = gr.ChatInterface(
|
| 165 |
+
respond,
|
| 166 |
+
additional_inputs=[
|
| 167 |
+
model_dropdown,
|
| 168 |
+
system_msg,
|
| 169 |
+
max_tokens,
|
| 170 |
+
temperature,
|
| 171 |
+
top_p,
|
| 172 |
+
login_btn
|
| 173 |
+
],
|
| 174 |
+
examples=[
|
| 175 |
+
["Explain quantum computing in simple terms"],
|
| 176 |
+
["Write a poem about AI"],
|
| 177 |
+
["How to learn programming?"],
|
| 178 |
+
["Tell me a joke"],
|
| 179 |
+
["What's the meaning of life?"],
|
| 180 |
+
],
|
| 181 |
+
cache_examples=False,
|
| 182 |
+
)
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|