| import gradio as gr |
| from huggingface_hub import InferenceClient |
|
|
| import os |
| import psycopg2 |
|
|
| """ |
| 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 |
| """ |
| client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") |
|
|
| try: |
| |
| conn = psycopg2.connect( |
| host=os.getenv("DB_HOST"), |
| port=os.getenv("DB_PORT"), |
| dbname=os.getenv("DB_NAME"), |
| user=os.getenv("DB_USER"), |
| password=os.getenv("DB_PASSWORD") |
| ) |
| print("Successfully connected to the database!") |
|
|
| |
| with conn.cursor() as cur: |
| cur.execute("SELECT version();") |
| db_version = cur.fetchone() |
| print(f"PostgreSQL version: {db_version[0]}") |
|
|
| insert_query = "INSERT INTO test (name) VALUES (%s);" |
| name_to_insert = "Marvin Minsky" |
| cur.execute(insert_query, (name_to_insert,)) |
| conn.commit() |
|
|
| |
| conn.close() |
| print("🔌 Connection closed.") |
| except Exception as e: |
| print("Database connection failed!") |
| print(f"Error: {e}") |
|
|
| def respond( |
| message, |
| history: list[tuple[str, str]], |
| system_message, |
| max_tokens, |
| temperature, |
| top_p, |
| ): |
| messages = [{"role": "system", "content": system_message}] |
|
|
| for val in history: |
| if val[0]: |
| messages.append({"role": "user", "content": val[0]}) |
| if val[1]: |
| messages.append({"role": "assistant", "content": val[1]}) |
|
|
| messages.append({"role": "user", "content": message}) |
|
|
| response = "" |
|
|
| for message in client.chat_completion( |
| messages, |
| max_tokens=max_tokens, |
| stream=True, |
| temperature=temperature, |
| top_p=top_p, |
| ): |
| token = message.choices[0].delta.content |
|
|
| response += token |
| yield response |
|
|
|
|
| def page1_fn(text, state): |
| state["data"] = text |
| return f"Page 1 saved: {text}", state |
|
|
| def page2_fn(num, state): |
| return f"Page 2 got from Page 1: {state.get('data', 'nothing')}, calculated: {num * 2}", state |
|
|
| with gr.Blocks() as demo: |
| state = gr.State(value={}) |
| gr.Markdown("# First demo test, hello") |
| with gr.Tabs(): |
| with gr.TabItem("Page 1"): |
| text_input = gr.Textbox(label="Enter text") |
| output1 = gr.Textbox() |
| gr.Button("Save").click(page1_fn, inputs=[text_input, state], outputs=[output1, state]) |
| with gr.TabItem("Page 2"): |
| num_input = gr.Number(label="Enter number") |
| output2 = gr.Textbox() |
| gr.Button("Submit").click(page2_fn, inputs=[num_input, state], outputs=[output2, state]) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|