|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("=========\nBegin import library\n") |
|
|
|
|
|
import random |
|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
import os |
|
from google import genai |
|
|
|
print("\nEnd import library\n=========") |
|
|
|
|
|
|
|
|
|
|
|
print("=========\nBegin definition Backend Logic\n") |
|
|
|
print("Create default API settings") |
|
|
|
google_API_key=os.getenv("GEMINI_API_KEY") |
|
|
|
client = genai.Client( |
|
api_key=google_API_key, |
|
http_options=genai.types.HttpOptions(api_version='v1alpha'), |
|
) |
|
print("Set default model") |
|
used_model = "gemini-2.5-flash-preview-04-17" |
|
|
|
print("Define test response work") |
|
def model_response(message, history): |
|
chat = client.chats.create(model=used_model, history=history) |
|
response = chat.send_message(message) |
|
return response |
|
|
|
print("Define test1 response work") |
|
def random_response(message, history): |
|
return random.choice(["Yes", "No"]) |
|
|
|
print("\nEnd definition Backend Logic\n=========") |
|
|
|
|
|
|
|
|
|
|
|
print("=========\nBegin definition User Interface (UI)\n") |
|
|
|
with gr.Blocks(theme=gr.themes.Soft(primary_hue="teal", secondary_hue="slate", neutral_hue="neutral")) as demo: |
|
print("Create visitor badge") |
|
gr.HTML("""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fzelk12%2FChat_interface_test_With_backend"> |
|
<img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fzelk12%2FChat_interface_test_With_backend&countColor=%23263759" /> |
|
</a>""") |
|
|
|
print("Create API block") |
|
with gr.Accordion( |
|
"API", |
|
open=False, |
|
): |
|
print("Create API key textbox row") |
|
with gr.Row(): |
|
print("Create API key textbox") |
|
Textbox_Block_API_key = gr.Textbox( |
|
label="API key", |
|
scale=4, |
|
) |
|
|
|
print("Create API block button row") |
|
with gr.Row(): |
|
print("Create API apply button") |
|
Button_Block_Apply_API_key = gr.Button( |
|
value="Apply", |
|
scale=1, |
|
) |
|
print("Create API reset button") |
|
Button_Block_Reset_API_key = gr.Button( |
|
value="Reset", |
|
scale=1, |
|
) |
|
|
|
print("Create API state markdown") |
|
Markdown_Block_API_key_State = gr.Markdown("API key State: False") |
|
|
|
|
|
|
|
print("Create provider dropdown") |
|
Dropdown_Block_Choose_provider = gr.Dropdown(label="Choose provider") |
|
|
|
|
|
|
|
print("Create provider state") |
|
Markdown_Block_Povider_State = gr.Markdown("Provider State: False") |
|
|
|
|
|
|
|
print("Create main chat window") |
|
ChatIntarface_Block_Main_chat_window = gr.ChatInterface(model_response, |
|
multimodal=True, |
|
chatbot=gr.Chatbot( |
|
label="output", |
|
), |
|
type="messages", |
|
textbox=gr.MultimodalTextbox( |
|
label="input", |
|
), |
|
editable=True, |
|
title="Chat interface test", |
|
save_history=True, |
|
) |
|
|
|
print("Create output token markdown") |
|
Markdown_Block_Output_token = gr.Markdown("Token in output: False") |
|
print("Create input token markdown") |
|
Markdown_Block_Input_token = gr.Markdown("Token in input: False") |
|
|
|
print("Create ssettings block") |
|
with gr.Accordion( |
|
"Settings", |
|
open=False, |
|
): |
|
print("Create model dropdown") |
|
Dropdown_Block_Choose_model = gr.Dropdown(label="Choose model") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Create system instructions textbox") |
|
Textbox_Block_System_instructions = gr.Textbox(label="System instructions",) |
|
print("Create slider model temperature") |
|
Slier_Block_Model_Temperature = gr.Slider(label="temperature", |
|
interactive=True, |
|
minimum=0, |
|
maximum=2, |
|
value=0.95) |
|
print("Create slider model topP") |
|
Slier_Block_Model_topP = gr.Slider(label="topP", |
|
interactive=True, |
|
minimum=0, |
|
maximum=1, |
|
value=0.5) |
|
print("Create slider model topK") |
|
Slier_Block_Model_topK = gr.Slider(label="topK", |
|
interactive=True, |
|
value=100) |
|
print("Create checkbox output stream") |
|
Checkbox_Block_Output_Stream = gr.Checkbox( |
|
label="Enable output stream" |
|
) |
|
print("Create checkbox Grounding with Google Search") |
|
Checkbox_Block_Google_Grounding_Search = gr.Checkbox( |
|
label="Grounding with Google Search" |
|
) |
|
|
|
print("\nEnd definition User Interface (UI)\n=========") |
|
|
|
print("=========\nBegin launch demo\n") |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|
|
print("\nEnd launch demo\n=========") |