rossiter78 commited on
Commit
72dab5c
·
verified ·
1 Parent(s): bb55854

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ # Configuration
6
+ LANGFLOW_API_URL = os.environ.get("LANGFLOW_API_URL", "https://rossiter78-langflowpoc.hf.space/api/v1/run/d53e1b2f-3572-40c8-84ab-725106ee858f")
7
+ #LANGFLOW_API_URL = "https://rossiter78-langflowpoc.hf.space/api/v1/run/d53e1b2f-3572-40c8-84ab-725106ee858f"
8
+ LANGFLOW_API_KEY = os.environ.get("LANGFLOW_API_KEY", "") # If needed
9
+ HF_API_KEY = os.environ.get("HF_API_KEY", "")
10
+
11
+ def call_langflow(message, history):
12
+ """
13
+ Call Langflow API and return the response
14
+ """
15
+ headers = {
16
+ "Content-Type": "application/json",
17
+ }
18
+
19
+ # Add API key if needed
20
+ if LANGFLOW_API_KEY:
21
+ headers["Authorization"] = f"Bearer {LANGFLOW_API_KEY}"
22
+ if HF_API_KEY:
23
+ headers["x-api-key"] = f"{HF_API_KEY}"
24
+
25
+
26
+ # Adjust this payload based on your Langflow API structure
27
+ payload = {
28
+ "input_value": message,
29
+ "output_type": "chat",
30
+ "input_type": "chat",
31
+ "tweaks": {}
32
+ }
33
+
34
+ try:
35
+ response = requests.post(
36
+ LANGFLOW_API_URL,
37
+ json=payload,
38
+ headers=headers,
39
+ timeout=30
40
+ )
41
+ response.raise_for_status()
42
+
43
+ # Parse response - adjust based on your API response structure
44
+ data = response.json()
45
+
46
+ # Common Langflow response structures:
47
+ # Option 1: data["outputs"][0]["outputs"][0]["results"]["message"]["text"]
48
+ # Option 2: data["result"]["message"]
49
+ # Adjust the following line based on your actual response:
50
+
51
+ bot_message = data["outputs"][0]["outputs"][0]["results"]["message"]["text"]
52
+ return bot_message
53
+
54
+ except requests.exceptions.RequestException as e:
55
+ return f"Error connecting to Langflow: {str(e)}"
56
+ except (KeyError, IndexError) as e:
57
+ return f"Error parsing response: {str(e)}\nResponse: {data}"
58
+
59
+ # Create Gradio Chat Interface
60
+ demo = gr.ChatInterface(
61
+ fn=call_langflow,
62
+ title="My Langflow Chatbot",
63
+ description="Chat with my AI assistant powered by Langflow",
64
+ examples=["Hello!", "What can you help me with?"],
65
+ theme=gr.themes.Soft(),
66
+ retry_btn=None,
67
+ undo_btn=None,
68
+ )
69
+
70
+ if __name__ == "__main__":
71
+ demo.launch()