Anaconda024 commited on
Commit
199c8a8
·
verified ·
1 Parent(s): 4bacd71
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -129,17 +129,14 @@ build_index()
129
  # API Endpoint for Hugging Face
130
  # ==========================
131
  def predict_api(question: str, hf_token: str, model_id: str):
132
- """API endpoint for Hugging Face Spaces"""
133
  answer = ask_question(question, hf_token, model_id)
134
  return {"data": [answer]}
135
 
136
- # ==========================
137
- # UI Function (separate from API)
138
- # ==========================
139
  def ui_ask_question(question: str, hf_token: str, model_id: str):
140
- """Function for the UI button"""
141
  answer = ask_question(question, hf_token, model_id)
142
- return answer # Just return the text for UI
143
 
144
  # ==========================
145
  # UI
@@ -153,14 +150,16 @@ with gr.Blocks() as demo:
153
  model_id = gr.Textbox(label="Model ID", value="google/flan-t5-base")
154
  with gr.Column():
155
  question = gr.Textbox(label="Ask a Question")
156
- answer = gr.Textbox(label="Answer")
157
  ask_btn = gr.Button("Ask")
158
 
159
  # Use the UI function for the button
160
  ask_btn.click(ui_ask_question, inputs=[question, hf_token, model_id], outputs=answer)
161
 
162
- # Expose the API function separately
163
- api = gr.Interface(
 
 
164
  fn=predict_api,
165
  inputs=[
166
  gr.Textbox(label="Question"),
@@ -169,8 +168,11 @@ api = gr.Interface(
169
  ],
170
  outputs=gr.JSON(),
171
  title="API Endpoint",
172
- description="Use this for API calls"
173
  )
174
 
175
- # Launch both the UI and API
176
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
129
  # API Endpoint for Hugging Face
130
  # ==========================
131
  def predict_api(question: str, hf_token: str, model_id: str):
132
+ """API endpoint for Hugging Face Spaces - returns JSON format"""
133
  answer = ask_question(question, hf_token, model_id)
134
  return {"data": [answer]}
135
 
 
 
 
136
  def ui_ask_question(question: str, hf_token: str, model_id: str):
137
+ """Function for the UI button - returns plain text"""
138
  answer = ask_question(question, hf_token, model_id)
139
+ return answer
140
 
141
  # ==========================
142
  # UI
 
150
  model_id = gr.Textbox(label="Model ID", value="google/flan-t5-base")
151
  with gr.Column():
152
  question = gr.Textbox(label="Ask a Question")
153
+ answer = gr.Textbox(label="Answer", interactive=False)
154
  ask_btn = gr.Button("Ask")
155
 
156
  # Use the UI function for the button
157
  ask_btn.click(ui_ask_question, inputs=[question, hf_token, model_id], outputs=answer)
158
 
159
+ # ==========================
160
+ # API Interface
161
+ # ==========================
162
+ api_interface = gr.Interface(
163
  fn=predict_api,
164
  inputs=[
165
  gr.Textbox(label="Question"),
 
168
  ],
169
  outputs=gr.JSON(),
170
  title="API Endpoint",
171
+ description="Use this endpoint for API calls via Postman"
172
  )
173
 
174
+ # ==========================
175
+ # Launch both interfaces
176
+ # ==========================
177
+ if __name__ == "__main__":
178
+ demo.launch(server_name="0.0.0.0", server_port=7860)