this code doesnt send the user input, and keeps sending "hi". import…
- User
PROMPT: user: this code doesnt send the user input, and keeps sending "hi". import requests import json import re def send_post_request(post_url, headers, payload): try: response = requests.post(post_url, headers=headers, json=payload) # Print the response text from the POST request #print("POST Response Text:") #print(response.text) # Print the raw response text if response.status_code == 200: print("POST request was successful!") else: print(f"POST request failed with status code: {response.status_code}") except requests.exceptions.RequestException as e: print(f"An error occurred during POST: {e}") def send_get_request(get_url, headers, params): try: response = requests.get(get_url, headers=headers, params=params) # Print the response text from the GET request #print("GET Response Text:") #print(response.text) # Print the raw response text if response.status_code == 200: #print("GET request was successful!") return response.text # Return the response for further processing else: print(f"GET request failed with status code: {response.status_code}") return None except requests.exceptions.RequestException as e: print(f"An error occurred during GET: {e}") return None def extract_ai_response(response_text): ai_responses = [] # Split the response based on "data: " parts = response_text.split('data: ') for part in parts: if 'process_completed' in part: try: # Extract the output JSON json_part = re.search(r'{"msg":"process_completed".*}', part) if json_part: output_data = json.loads(json_part.group(0)) # Get the AI message from the output field ai_messages = output_data['output']['data'] for message in ai_messages: # It seems the AI response is among the nested lists if isinstance(message, list) and len(message) > 0: # The second item in the first sub-list seems to be the response ai_response = message[0][1] ai_responses.append(ai_response) except (ValueError, IndexError) as e: print(f"Error parsing response: {e}") return ai_responses if name == "main": # Prepare URLs post_url = "https://yuntian-deng-chatgpt4.hf.space/queue/join" get_url = "https://yuntian-deng-chatgpt4.hf.space/queue/data" # Headers for the requests headers = { 'Accept': 'text/event-stream', 'Accept-Encoding': 'gzip, deflate, br, zstd', 'Accept-Language': 'en-US,en;q=0.7', 'Cache-Control': 'no-cache', 'Content-Type': 'application/json', 'Pragma': 'no-cache', 'Referer': '', 'Sec-CH-UA': '"Brave";v="129", "Not=A?Brand";v="8", "Chromium";v="129"', 'Sec-CH-UA-Mobile': '?0', 'Sec-CH-UA-Platform': '"Windows"', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'Sec-GPC': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36' } # User input for the payload hi_input = input("Please enter a text input (for 'hi'): ") # Payload for the POST request payload = { "data": [hi_input, 1, 1, 0, [], []], "event_data": None, "fn_index": 3, "session_hash": "26vb4jk6379", "trigger_id": 5 } # Send the POST request first send_post_request(post_url, headers, payload) # Prepare parameters for the GET request, including session_hash params = { 'session_hash': '26vb4jk6379' } # Then send the GET request response_text = send_get_request(get_url, headers, params) # Extract the AI response from the GET response if response_text: ai_responses = extract_ai_response(response_text) if ai_responses: #print("AI Responses:") for response in ai_responses: print(response) else: print("Could not extract AI responses.")
- Assistant