Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
from argparse import RawTextHelpFormatter
|
| 4 |
+
import requests
|
| 5 |
+
from typing import Optional
|
| 6 |
+
import warnings
|
| 7 |
+
try:
|
| 8 |
+
from langflow.load import upload_file
|
| 9 |
+
except ImportError:
|
| 10 |
+
warnings.warn("Langflow provides a function to help you upload files to the flow. Please install langflow to use it.")
|
| 11 |
+
upload_file = None
|
| 12 |
+
|
| 13 |
+
BASE_API_URL = "https://petchatflow-langflow.hf.space"
|
| 14 |
+
FLOW_ID = "f4bbfb02-e3a2-473a-ad03-50bd6a281c15"
|
| 15 |
+
ENDPOINT = "" # You can set a specific endpoint name in the flow settings
|
| 16 |
+
|
| 17 |
+
# You can tweak the flow by adding a tweaks dictionary
|
| 18 |
+
# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}}
|
| 19 |
+
TWEAKS = {
|
| 20 |
+
"ChatInput-QGSwh": {},
|
| 21 |
+
"AstraVectorStoreComponent-qG5nv": {},
|
| 22 |
+
"ParseData-nNfOY": {},
|
| 23 |
+
"Prompt-mkSCp": {},
|
| 24 |
+
"ChatOutput-sJAn8": {},
|
| 25 |
+
"SplitText-LMDKi": {},
|
| 26 |
+
"File-vpDZe": {},
|
| 27 |
+
"AstraVectorStoreComponent-RTQUj": {},
|
| 28 |
+
"OpenAIEmbeddings-HXWCd": {},
|
| 29 |
+
"OpenAIEmbeddings-k7zcb": {},
|
| 30 |
+
"OpenAIModel-8oPgP": {}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
def run_flow(message: str,
|
| 34 |
+
endpoint: str,
|
| 35 |
+
output_type: str = "chat",
|
| 36 |
+
input_type: str = "chat",
|
| 37 |
+
tweaks: Optional[dict] = None,
|
| 38 |
+
api_key: Optional[str] = None) -> dict:
|
| 39 |
+
"""
|
| 40 |
+
Run a flow with a given message and optional tweaks.
|
| 41 |
+
|
| 42 |
+
:param message: The message to send to the flow
|
| 43 |
+
:param endpoint: The ID or the endpoint name of the flow
|
| 44 |
+
:param tweaks: Optional tweaks to customize the flow
|
| 45 |
+
:return: The JSON response from the flow
|
| 46 |
+
"""
|
| 47 |
+
api_url = f"{BASE_API_URL}/api/v1/run/{endpoint}"
|
| 48 |
+
|
| 49 |
+
payload = {
|
| 50 |
+
"input_value": message,
|
| 51 |
+
"output_type": output_type,
|
| 52 |
+
"input_type": input_type,
|
| 53 |
+
}
|
| 54 |
+
headers = None
|
| 55 |
+
if tweaks:
|
| 56 |
+
payload["tweaks"] = tweaks
|
| 57 |
+
if api_key:
|
| 58 |
+
headers = {"x-api-key": api_key}
|
| 59 |
+
response = requests.post(api_url, json=payload, headers=headers)
|
| 60 |
+
return response.json()
|
| 61 |
+
|
| 62 |
+
def main():
|
| 63 |
+
parser = argparse.ArgumentParser(description="""Run a flow with a given message and optional tweaks.
|
| 64 |
+
Run it like: python <your file>.py "your message here" --endpoint "your_endpoint" --tweaks '{"key": "value"}'""",
|
| 65 |
+
formatter_class=RawTextHelpFormatter)
|
| 66 |
+
parser.add_argument("message", type=str, help="The message to send to the flow")
|
| 67 |
+
parser.add_argument("--endpoint", type=str, default=ENDPOINT or FLOW_ID, help="The ID or the endpoint name of the flow")
|
| 68 |
+
parser.add_argument("--tweaks", type=str, help="JSON string representing the tweaks to customize the flow", default=json.dumps(TWEAKS))
|
| 69 |
+
parser.add_argument("--api_key", type=str, help="API key for authentication", default=None)
|
| 70 |
+
parser.add_argument("--output_type", type=str, default="chat", help="The output type")
|
| 71 |
+
parser.add_argument("--input_type", type=str, default="chat", help="The input type")
|
| 72 |
+
parser.add_argument("--upload_file", type=str, help="Path to the file to upload", default=None)
|
| 73 |
+
parser.add_argument("--components", type=str, help="Components to upload the file to", default=None)
|
| 74 |
+
|
| 75 |
+
args = parser.parse_args()
|
| 76 |
+
try:
|
| 77 |
+
tweaks = json.loads(args.tweaks)
|
| 78 |
+
except json.JSONDecodeError:
|
| 79 |
+
raise ValueError("Invalid tweaks JSON string")
|
| 80 |
+
|
| 81 |
+
if args.upload_file:
|
| 82 |
+
if not upload_file:
|
| 83 |
+
raise ImportError("Langflow is not installed. Please install it to use the upload_file function.")
|
| 84 |
+
elif not args.components:
|
| 85 |
+
raise ValueError("You need to provide the components to upload the file to.")
|
| 86 |
+
tweaks = upload_file(file_path=args.upload_file, host=BASE_API_URL, flow_id=args.endpoint, components=[args.components], tweaks=tweaks)
|
| 87 |
+
|
| 88 |
+
response = run_flow(
|
| 89 |
+
message=args.message,
|
| 90 |
+
endpoint=args.endpoint,
|
| 91 |
+
output_type=args.output_type,
|
| 92 |
+
input_type=args.input_type,
|
| 93 |
+
tweaks=tweaks,
|
| 94 |
+
api_key=args.api_key
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
print(json.dumps(response, indent=2))
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|