rr19tech commited on
Commit
5b68cd9
·
verified ·
1 Parent(s): e701dda

Updated the code to support multiple actions

Browse files
Files changed (1) hide show
  1. app.py +47 -1
app.py CHANGED
@@ -9,7 +9,7 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
9
  # Note: Use the direct .hf.space URL
10
  PRIVATE_API_URL = "https://rr19tech-plnb-demo.hf.space/get_content"
11
 
12
-
13
  def call_private_api(user_input):
14
  # Pass the token in the Authorization header
15
  headers = {
@@ -42,3 +42,49 @@ def call_private_api(user_input):
42
  # Simple UI to test it
43
  demo = gr.Interface(fn=call_private_api, inputs="text", outputs="json").launch()
44
  #demo.queue().launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  # Note: Use the direct .hf.space URL
10
  PRIVATE_API_URL = "https://rr19tech-plnb-demo.hf.space/get_content"
11
 
12
+ '''
13
  def call_private_api(user_input):
14
  # Pass the token in the Authorization header
15
  headers = {
 
42
  # Simple UI to test it
43
  demo = gr.Interface(fn=call_private_api, inputs="text", outputs="json").launch()
44
  #demo.queue().launch()
45
+ '''
46
+ #changing this to a server method
47
+ from gradio import Server
48
+
49
+ # Initialize the server
50
+ app = Server()
51
+
52
+ # Define Method 1: Triggers when client calls /gradio_api/call/update_data
53
+ @app.api(name="call_private_api")
54
+ def call_private_api(user_input):
55
+ # Pass the token in the Authorization header
56
+ headers = {
57
+ "Authorization": f"Bearer {HF_TOKEN}",
58
+ #"Content-Type": "text/plain"
59
+ "Content-Type": "application/json"
60
+ }
61
+
62
+ print(f"User input is: {user_input}")
63
+
64
+ # We take that string and put it into a GET request
65
+ try:
66
+ response = requests.get(
67
+ PRIVATE_API_URL,
68
+ headers=headers,
69
+ params={"text": user_input},
70
+ timeout=300,
71
+ )
72
+ content_type = response.headers.get("content-type")
73
+ print(f"DEBUG: Status {response.status_code} | Content-Type: {content_type}")
74
+
75
+ if response.status_code == 200:
76
+ #return response.json()
77
+ return response.text # workaround
78
+ else:
79
+ return {"error": f"Status {response.status_code}", "detail": response.text}
80
+ except Exception as e:
81
+ return {"error": f"Connection failed: {str(e)}"}
82
+
83
+ # Define Method 2: Triggers when client calls /gradio_api/call/process_action
84
+ @app.api(name="user_feedback")
85
+ def process_user_feedback(user_input):
86
+ # Logic for different client actions
87
+ return f"user feedback received '{user_input}' processed on server."
88
+
89
+ app.launch()
90
+