yhraje commited on
Commit
e666bbb
Β·
verified Β·
1 Parent(s): 0c883b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -50
app.py CHANGED
@@ -1,50 +1,58 @@
1
- import requests
2
- import gradio as gr
3
-
4
- # API Configuration
5
- api_key = "sk-6Ng7YDAkl0D5z20vAh7tPkmWn5hCTcZGIcl_wRiQ5NQ" # replace with your actual key
6
- url = "http://localhost:7860/api/v1/run/30d9ce86-194f-44ba-a15a-f822c3ac4f57" # LangFlow endpoint
7
-
8
- def query_langflow(user_input):
9
- payload = {
10
- "output_type": "chat",
11
- "input_type": "chat",
12
- "input_value": user_input
13
- }
14
-
15
- headers = {
16
- "Content-Type": "application/json",
17
- "x-api-key": api_key
18
- }
19
-
20
- try:
21
- response = requests.post(url, json=payload, headers=headers)
22
- response.raise_for_status()
23
- data = response.json()
24
-
25
- # Adjust based on actual LangFlow response structure
26
- if isinstance(data, dict):
27
- return data.get("outputs", data)
28
- return str(data)
29
-
30
- except requests.exceptions.RequestException as e:
31
- return f"❌ API request failed: {e}"
32
- except ValueError as e:
33
- return f"⚠️ Error parsing response: {e}"
34
-
35
- # Gradio UI
36
- with gr.Blocks() as demo:
37
- gr.Markdown("## πŸš€ LangFlow Chat with Gradio")
38
-
39
- chatbot = gr.Chatbot()
40
- msg = gr.Textbox(placeholder="Ask something...")
41
-
42
- def chat(user_input, history):
43
- response = query_langflow(user_input)
44
- history.append((user_input, response))
45
- return history, ""
46
-
47
- msg.submit(chat, [msg, chatbot], [chatbot, msg])
48
-
49
- # πŸš€ This will show BOTH: local URL + public .gradio.live URL
50
- demo.launch(server_name="127.0.0.1", server_port=7861, share=True)
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import gradio as gr
3
+ from pyngrok import ngrok
4
+ import os
5
+
6
+ # πŸ”‘ Set NGROK token
7
+ os.environ["NGROK_AUTHTOKEN"] = "33Jqcou6HvfbAEgNa8h20QTpyWx_H4VDZ6RewUx96owPmgdC"
8
+ ngrok.set_auth_token(os.environ["NGROK_AUTHTOKEN"])
9
+
10
+ # API Configuration
11
+ api_key = "sk-6Ng7YDAkl0D5z20vAh7tPkmWn5hCTcZGIcl_wRiQ5NQ"
12
+ url = "http://localhost:7860/api/v1/run/30d9ce86-194f-44ba-a15a-f822c3ac4f57"
13
+
14
+ def query_langflow(user_input):
15
+ payload = {
16
+ "output_type": "chat",
17
+ "input_type": "chat",
18
+ "input_value": user_input
19
+ }
20
+
21
+ headers = {
22
+ "Content-Type": "application/json",
23
+ "x-api-key": api_key
24
+ }
25
+
26
+ try:
27
+ response = requests.post(url, json=payload, headers=headers)
28
+ response.raise_for_status()
29
+ data = response.json()
30
+ if isinstance(data, dict):
31
+ return data.get("outputs", data)
32
+ return str(data)
33
+
34
+ except requests.exceptions.RequestException as e:
35
+ return f"❌ API request failed: {e}"
36
+ except ValueError as e:
37
+ return f"⚠️ Error parsing response: {e}"
38
+
39
+ # Gradio UI
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown("## πŸš€ LangFlow Chat with Gradio + Ngrok")
42
+
43
+ chatbot = gr.Chatbot()
44
+ msg = gr.Textbox(placeholder="Ask something...")
45
+
46
+ def chat(user_input, history):
47
+ response = query_langflow(user_input)
48
+ history.append((user_input, response))
49
+ return history, ""
50
+
51
+ msg.submit(chat, [msg, chatbot], [chatbot, msg])
52
+
53
+ # Launch Gradio on fixed port
54
+ port = 7861
55
+ public_url = ngrok.connect(port, "http").public_url
56
+ print(f"🌍 Public URL (Ngrok): {public_url}")
57
+
58
+ demo.launch(server_name="0.0.0.0", server_port=port)