Th3Nic3Guy commited on
Commit
075f63b
·
verified ·
1 Parent(s): 06eba4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """gradio app for Chat Interface for DataStax Langflow calls"""
2
+
3
+ import json
4
+ from typing import Optional, Sequence, Tuple
5
+ from uuid import uuid4
6
+ import requests
7
+ import gradio as gr
8
+ import os
9
+
10
+ BASE_API_URL = "https://api.langflow.astra.datastax.com"
11
+ LANGFLOW_ID = "e99b525a-84dd-4cf2-bac6-8e5ebc6a7d17"
12
+ FLOW_ID = "bfbf7237-50dd-40dd-baba-cc680288d788"
13
+ APPLICATION_TOKEN = os.getenv('DATASTAX_TOKEN')
14
+
15
+ ENDPOINT = 'ai_traveller'
16
+
17
+ TWEAKS = {
18
+ "Memory-fCuCs": {
19
+ "n_messages": 100,
20
+ "order": "Ascending",
21
+ "sender": "Machine and User",
22
+ "sender_name": "",
23
+ "template": "{sender_name}: {text}"
24
+ },
25
+ "ChatInput-PN5E4": {},
26
+ "Prompt-vGvVG": {},
27
+ "GoogleGenerativeAIModel-6n0Ft": {},
28
+ "ChatOutput-HjfF1": {}
29
+ }
30
+
31
+
32
+ def call_travel_ai(
33
+ message: str,
34
+ history: Sequence[Tuple[str, str]],
35
+ session_id: str,
36
+ endpoint: str = ENDPOINT,
37
+ output_type: str = "chat",
38
+ input_type: str = "chat",
39
+ tweaks: Optional[dict] = None,
40
+ application_token: Optional[str] = APPLICATION_TOKEN
41
+ ) -> dict:
42
+ """
43
+ Run a flow with a given message and optional tweaks.
44
+
45
+ :param message: The message to send to the flow
46
+ :param endpoint: The ID or the endpoint name of the flow
47
+ :param tweaks: Optional tweaks to customize the flow
48
+ :return: The JSON response from the flow
49
+ """
50
+ api_url = f"{BASE_API_URL}/lf/{LANGFLOW_ID}/api/v1/run/{endpoint}"
51
+
52
+ payload = {
53
+ 'session_id': session_id,
54
+ "input_value": message,
55
+ "output_type": output_type,
56
+ "input_type": input_type,
57
+ }
58
+ headers = None
59
+ if tweaks:
60
+ payload["tweaks"] = tweaks
61
+ if application_token:
62
+ headers = {
63
+ "Authorization": "Bearer " + application_token,
64
+ "Content-Type": "application/json"
65
+ }
66
+ response = requests.post(
67
+ api_url,
68
+ json=payload,
69
+ headers=headers,
70
+ timeout=360
71
+ )
72
+ response = response.json()
73
+ resp_message = ''
74
+ for resp in response['outputs']:
75
+ for _resp in resp['outputs']:
76
+ for message in _resp['messages']:
77
+ resp_message = message['message']
78
+
79
+ return resp_message
80
+
81
+ def generate_session_id():
82
+ return str(uuid4())
83
+
84
+ def render_value(value):
85
+ return value
86
+
87
+ with gr.Blocks() as demo:
88
+ session_id = gr.Textbox(
89
+ value=generate_session_id,
90
+ visible=False,
91
+ interactive=False,
92
+ label="Session ID",
93
+ )
94
+
95
+ chat_interface = gr.ChatInterface(
96
+ call_travel_ai,
97
+ type='messages',
98
+ title=f"AI Travel Partner",
99
+ additional_inputs=[session_id],
100
+ autofocus=True,
101
+ fill_height=True,
102
+ )
103
+
104
+ demo.launch(share=False, debug=True,)