Update app.py
Browse files
app.py
CHANGED
|
@@ -642,27 +642,27 @@ def build_demo():
|
|
| 642 |
|
| 643 |
# --- API endpoint: /chat ---
|
| 644 |
api_interface = gr.Interface(
|
| 645 |
-
fn=
|
| 646 |
-
inputs=[gr.
|
| 647 |
outputs="text",
|
| 648 |
-
allow_flagging="never",
|
| 649 |
api_name="/chat"
|
| 650 |
)
|
| 651 |
|
| 652 |
-
|
|
|
|
| 653 |
token = request.headers.get("x-api-token", "")
|
| 654 |
if token != "123456789Rihem":
|
| 655 |
-
return "❌ Unauthorized
|
| 656 |
|
| 657 |
dummy_state = init_state()
|
| 658 |
-
dummy_state.set_system_message("You are a
|
| 659 |
-
dummy_state.append_message(Conversation.USER, question
|
| 660 |
dummy_state.skip_next = False
|
| 661 |
|
| 662 |
worker_addr = os.environ.get("WORKER_ADDR", "")
|
| 663 |
api_token = os.environ.get("API_TOKEN", "")
|
| 664 |
headers = {
|
| 665 |
-
"Authorization": f"Bearer {api_token}",
|
| 666 |
"Content-Type": "application/json"
|
| 667 |
}
|
| 668 |
|
|
@@ -671,7 +671,7 @@ def http_api_infer(image, question, request: gr.Request):
|
|
| 671 |
|
| 672 |
pload = {
|
| 673 |
"model": "InternVL2.5-78B",
|
| 674 |
-
"messages": dummy_state.get_prompt_v2(inlude_image=
|
| 675 |
"temperature": 0.2,
|
| 676 |
"top_p": 0.7,
|
| 677 |
"max_tokens": 1024,
|
|
@@ -684,7 +684,8 @@ def http_api_infer(image, question, request: gr.Request):
|
|
| 684 |
reply = response.json()["choices"][0]["message"]["content"]
|
| 685 |
return reply
|
| 686 |
except Exception as e:
|
| 687 |
-
return f"Error
|
|
|
|
| 688 |
|
| 689 |
if __name__ == "__main__":
|
| 690 |
parser = argparse.ArgumentParser()
|
|
|
|
| 642 |
|
| 643 |
# --- API endpoint: /chat ---
|
| 644 |
api_interface = gr.Interface(
|
| 645 |
+
fn=http_api_infer,
|
| 646 |
+
inputs=[gr.Textbox(label="Question")],
|
| 647 |
outputs="text",
|
|
|
|
| 648 |
api_name="/chat"
|
| 649 |
)
|
| 650 |
|
| 651 |
+
|
| 652 |
+
def http_api_infer(question: str, request: gr.Request):
|
| 653 |
token = request.headers.get("x-api-token", "")
|
| 654 |
if token != "123456789Rihem":
|
| 655 |
+
return "❌ Unauthorized: Invalid token."
|
| 656 |
|
| 657 |
dummy_state = init_state()
|
| 658 |
+
dummy_state.set_system_message("You are a helpful assistant.")
|
| 659 |
+
dummy_state.append_message(Conversation.USER, question)
|
| 660 |
dummy_state.skip_next = False
|
| 661 |
|
| 662 |
worker_addr = os.environ.get("WORKER_ADDR", "")
|
| 663 |
api_token = os.environ.get("API_TOKEN", "")
|
| 664 |
headers = {
|
| 665 |
+
"Authorization": f"Bearer {api_token}",
|
| 666 |
"Content-Type": "application/json"
|
| 667 |
}
|
| 668 |
|
|
|
|
| 671 |
|
| 672 |
pload = {
|
| 673 |
"model": "InternVL2.5-78B",
|
| 674 |
+
"messages": dummy_state.get_prompt_v2(inlude_image=False),
|
| 675 |
"temperature": 0.2,
|
| 676 |
"top_p": 0.7,
|
| 677 |
"max_tokens": 1024,
|
|
|
|
| 684 |
reply = response.json()["choices"][0]["message"]["content"]
|
| 685 |
return reply
|
| 686 |
except Exception as e:
|
| 687 |
+
return f"❌ Error: {str(e)}"
|
| 688 |
+
|
| 689 |
|
| 690 |
if __name__ == "__main__":
|
| 691 |
parser = argparse.ArgumentParser()
|