rr19tech commited on
Commit
7e4d4dd
·
verified ·
1 Parent(s): 3fa0fdb
Files changed (1) hide show
  1. app.py +20 -120
app.py CHANGED
@@ -1,132 +1,32 @@
1
- import os
2
- import requests
3
- import gradio as gr # Or whatever UI you use
4
- from fastapi import Body
5
 
6
- # 1. Pull your secret token
7
- HF_TOKEN = os.environ.get("HF_TOKEN")
8
- DB_HF_TOKEN = os.environ.get("DB_Write_Token")
9
 
10
- # 2. Define the full URL to your private FastAPI endpoint
11
- # Note: Use the direct .hf.space URL
12
- PRIVATE_API_URL = "https://rr19tech-plnb-demo.hf.space/get_content"
13
- FEEDBACK_API_URL = "https://rr19tech-plnb-demo.hf.space/add_user_comment"
14
- '''
15
- def call_private_api(user_input):
16
- # Pass the token in the Authorization header
17
- headers = {
18
- "Authorization": f"Bearer {HF_TOKEN}",
19
- #"Content-Type": "text/plain"
20
- "Content-Type": "application/json"
21
- }
22
 
23
- print(f"User input is: {user_input}")
 
 
 
 
 
 
 
24
 
25
- # We take that string and put it into a GET request
26
- try:
27
- response = requests.get(
28
- PRIVATE_API_URL,
29
- headers=headers,
30
- params={"text": user_input},
31
- timeout=300,
32
- )
33
- content_type = response.headers.get("content-type")
34
- print(f"DEBUG: Status {response.status_code} | Content-Type: {content_type}")
35
-
36
- if response.status_code == 200:
37
- #return response.json()
38
- return response.text # workaround
39
- else:
40
- return {"error": f"Status {response.status_code}", "detail": response.text}
41
- except Exception as e:
42
- return {"error": f"Connection failed: {str(e)}"}
43
 
44
- # Simple UI to test it
45
- demo = gr.Interface(fn=call_private_api, inputs="text", outputs="json").launch()
46
- #demo.queue().launch()
47
- '''
48
 
49
- # Pass the token in the Authorization header
50
 
51
- #changing this to a server method
52
- from gradio import Server
53
- #from fastapi.middleware.cors import CORSMiddleware
54
 
55
- # Initialize the server
56
- app = Server()
57
- '''
58
- app.add_middleware(
59
- CORSMiddleware,
60
- # Replace with the EXACT domain where your JS code is running (include http/https)
61
- # DO NOT USE ["*"] here, as browsers block credentials paired with wildcards
62
- allow_origins=["http://localhost:8000", "https://your-frontend-app.com"],
63
- allow_credentials=True, # Matches the incoming JS request flag
64
- allow_methods=["*"],
65
- allow_headers=["*"],
66
- )
67
- '''
68
- # Define Method 1: Triggers when client calls /gradio_api/call/update_data
69
- #@app.api(name="call_private_api")
70
- @app.post("/call_private_api")
71
- async def call_private_api(user_input: str) -> dict:
72
- #def call_private_api(user_input: str = Body(..., embed=True)) -> dict:
73
 
74
- headers = {
75
- "Authorization": f"Bearer {HF_TOKEN}",
76
- #"Content-Type": "text/plain"
77
- "Content-Type": "application/json"
78
- }
79
- print(f"User input is: {user_input}")
80
-
81
- # We take that string and put it into a GET request
82
- try:
83
- response = requests.get(
84
- PRIVATE_API_URL,
85
- headers=headers,
86
- params={"text": user_input},
87
- timeout=300,
88
- )
89
- content_type = response.headers.get("content-type")
90
- print(f"DEBUG: Status {response.status_code} | Content-Type: {content_type}")
91
-
92
- if response.status_code == 200:
93
- return response.json()
94
- #return response.text # workaround
95
- else:
96
- return {"error": f"Status {response.status_code}", "detail": response.text}
97
- except Exception as e:
98
- return {"error": f"Connection failed: {str(e)}"}
99
 
100
- # Define Method 2: User feedback
101
-
102
- #@app.api(name="user_feedback")
103
- @app.post("/user_feedback")
104
- async def process_user_feedback(user_input: str) -> dict:
105
-
106
- headers = {
107
- "Authorization": f"Bearer {DB_HF_TOKEN}",
108
- #"Content-Type": "text/plain"
109
- "Content-Type": "application/json"
110
- }
111
- print(f"User feedback input is : {user_input}")
112
- try:
113
- response = requests.get(
114
- FEEDBACK_API_URL,
115
- headers=headers,
116
- params={"text": user_input},
117
- timeout=300,
118
- )
119
- content_type = response.headers.get("content-type")
120
- print(f"DEBUG: Status {response.status_code} | Content-Type: {content_type}")
121
-
122
- if response.status_code == 200:
123
- return response.json()
124
- #return response.text # workaround
125
- else:
126
- return {"error": f"Status {response.status_code}", "detail": response.text}
127
- except Exception as e:
128
- return {"error": f"Connection failed: {str(e)}"}
129
-
130
 
131
- app.launch()
132
 
 
1
+ from fastapi import FastAPI
2
+ import gradio as gr
3
+ from pydantic import BaseModel
 
4
 
5
+ app = FastAPI()
 
 
6
 
7
+ # 1. Define the incoming JSON structure
8
+ class ApiInput(BaseModel):
9
+ user_string: str # The key name you want to use in your JSON payload
 
 
 
 
 
 
 
 
 
10
 
11
+ # 2. Setup your POST route
12
+ @app.post("/call_private_api")
13
+ def call_private_api(payload: ApiInput):
14
+ # Extract the string value from the request
15
+ input_text = payload.user_string
16
+
17
+ # Run your custom processing logic here
18
+ processed_result = f"Received and processed: {input_text}"
19
 
20
+ return {"result": processed_result}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ # 3. Mount your standard Gradio interface
23
+ with gr.Blocks() as demo:
24
+ gr.Markdown("# My Space Interface")
 
25
 
26
+ app = gr.mount_gradio_app(app, demo, path="/")
27
 
 
 
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
 
32