Commit ·
f458bd2
1
Parent(s): e86e0b2
updating code
Browse files
app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""gradio app for Chat Interface for DataStax Langflow calls"""
|
| 2 |
|
| 3 |
-
from typing import Optional, Sequence, Tuple
|
| 4 |
-
from uuid import uuid4
|
| 5 |
-
import os
|
| 6 |
-
from requests.exceptions import ConnectTimeout, ReadTimeout
|
| 7 |
-
from retry import retry
|
| 8 |
-
import requests
|
| 9 |
-
import gradio as gr
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
|
@@ -35,7 +37,7 @@ TWEAKS = {
|
|
| 35 |
|
| 36 |
|
| 37 |
@retry((ConnectTimeout, ReadTimeout), tries=3, delay=2)
|
| 38 |
-
def
|
| 39 |
message: str,
|
| 40 |
history: Sequence[Tuple[str, str]],
|
| 41 |
local_storage: str,
|
|
@@ -77,7 +79,7 @@ def call_travel_ai(
|
|
| 77 |
headers=headers,
|
| 78 |
timeout=120,
|
| 79 |
)
|
| 80 |
-
print(response.content)
|
| 81 |
response = response.json()
|
| 82 |
resp_message = ''
|
| 83 |
try:
|
|
@@ -101,25 +103,52 @@ with gr.Blocks() as demo:
|
|
| 101 |
with gr.Column():
|
| 102 |
gr.Image(
|
| 103 |
"./aem_logo.jpeg",
|
| 104 |
-
width=300,
|
|
|
|
|
|
|
| 105 |
show_fullscreen_button=False,
|
| 106 |
show_label=False,
|
| 107 |
interactive=False,
|
| 108 |
show_share_button=False,
|
| 109 |
)
|
| 110 |
|
| 111 |
-
session = gr.
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
with gr.Accordion(
|
| 114 |
'Chat With Agent',
|
| 115 |
) as chat:
|
| 116 |
chat_interface = gr.ChatInterface(
|
| 117 |
-
|
| 118 |
type='messages',
|
| 119 |
-
title="AI
|
| 120 |
additional_inputs=[session],
|
| 121 |
autofocus=True,
|
| 122 |
fill_height=True,
|
| 123 |
)
|
| 124 |
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
from retry import retry
|
| 5 |
+
from requests.exceptions import ConnectTimeout, ReadTimeout
|
| 6 |
+
import os
|
| 7 |
+
from uuid import uuid4
|
| 8 |
+
from typing import Optional, Sequence, Tuple
|
| 9 |
+
|
| 10 |
+
|
| 11 |
"""gradio app for Chat Interface for DataStax Langflow calls"""
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
@retry((ConnectTimeout, ReadTimeout), tries=3, delay=2)
|
| 40 |
+
def call_ai(
|
| 41 |
message: str,
|
| 42 |
history: Sequence[Tuple[str, str]],
|
| 43 |
local_storage: str,
|
|
|
|
| 79 |
headers=headers,
|
| 80 |
timeout=120,
|
| 81 |
)
|
| 82 |
+
print(response, response.content)
|
| 83 |
response = response.json()
|
| 84 |
resp_message = ''
|
| 85 |
try:
|
|
|
|
| 103 |
with gr.Column():
|
| 104 |
gr.Image(
|
| 105 |
"./aem_logo.jpeg",
|
| 106 |
+
width=300,
|
| 107 |
+
label="AEM",
|
| 108 |
+
show_download_button=False,
|
| 109 |
show_fullscreen_button=False,
|
| 110 |
show_label=False,
|
| 111 |
interactive=False,
|
| 112 |
show_share_button=False,
|
| 113 |
)
|
| 114 |
|
| 115 |
+
# session = gr.State(["", ], time_to_live=24 * 60*60)
|
| 116 |
+
session = gr.BrowserState(None, storage_key="session_id")
|
| 117 |
+
show_session = gr.Textbox(
|
| 118 |
+
visible=False,
|
| 119 |
+
interactive=False,
|
| 120 |
+
label="Session ID",
|
| 121 |
+
)
|
| 122 |
with gr.Accordion(
|
| 123 |
'Chat With Agent',
|
| 124 |
) as chat:
|
| 125 |
chat_interface = gr.ChatInterface(
|
| 126 |
+
call_ai,
|
| 127 |
type='messages',
|
| 128 |
+
title="AI Beauty Partner",
|
| 129 |
additional_inputs=[session],
|
| 130 |
autofocus=True,
|
| 131 |
fill_height=True,
|
| 132 |
)
|
| 133 |
|
| 134 |
+
def initialize_session(existing_id):
|
| 135 |
+
"""
|
| 136 |
+
Runs ONCE per browser session on page load.
|
| 137 |
+
Checks if an ID already exists in BrowserState (local storage).
|
| 138 |
+
If not, generates a new one.
|
| 139 |
+
Returns the ID to populate the BrowserState and other components.
|
| 140 |
+
"""
|
| 141 |
+
if existing_id:
|
| 142 |
+
print(f"Found existing ID in BrowserState: {existing_id}")
|
| 143 |
+
return existing_id, existing_id
|
| 144 |
+
else:
|
| 145 |
+
new_id = generate_session_id()
|
| 146 |
+
print(f"No existing ID found, returning NEW ID: {new_id}")
|
| 147 |
+
return new_id, new_id
|
| 148 |
+
|
| 149 |
+
demo.load( # pylint: disable=no-member
|
| 150 |
+
initialize_session,
|
| 151 |
+
[session],
|
| 152 |
+
outputs=[session, show_session],
|
| 153 |
+
)
|
| 154 |
+
demo.launch(share=False, debug=True,)
|