Niansuh commited on
Commit
b4b4823
·
verified ·
1 Parent(s): 7098fc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -91
app.py CHANGED
@@ -1,110 +1,46 @@
1
  import requests
2
- from flask import Flask, request, jsonify
3
- import logging
4
  import json
5
 
6
- # Initialize Flask app
7
- app = Flask(__name__)
8
 
9
- # Set up logging for debugging purposes
10
- logging.basicConfig(level=logging.INFO)
11
-
12
- # Define the endpoint to interact with PizzaGPT API
13
- PIZZAGPT_API_URL = 'https://www.pizzagpt.it/api/chatx-completion'
14
-
15
- # PizzaGPT API headers (adjusted to include necessary ones)
16
  headers = {
17
- 'accept': 'application/json',
18
  'accept-language': 'en-US,en;q=0.9,ru;q=0.8',
19
  'content-type': 'application/json',
20
- 'cookie': 'i18n_redirected=en; _ga=GA1.1.976983986.1732021410; uid_dm=b6b6d425-4a02-7d8f-3e07-cbd6a0ce24f4; v1st_dm=47933c38-0f51-1931-367d-0214ede5d03f;',
21
- 'origin': 'https://www.pizzagpt.it',
22
  'priority': 'u=1, i',
23
- 'referer': 'https://www.pizzagpt.it/en',
24
  'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
25
  'sec-ch-ua-mobile': '?0',
26
  'sec-ch-ua-platform': '"Windows"',
27
  'sec-fetch-dest': 'empty',
28
  'sec-fetch-mode': 'cors',
29
- 'sec-fetch-site': 'same-origin',
30
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
31
- 'x-secret': 'Marinara', # Secret key for PizzaGPT API
32
  }
33
 
34
- @app.route('/v1/chat/completions', methods=['POST'])
35
- def chat_completions():
36
- try:
37
- # Log incoming request data for debugging
38
- data = request.get_json()
39
- app.logger.info(f"Received request data: {json.dumps(data, indent=2)}")
40
-
41
- # Extract the 'messages' field from the incoming request
42
- messages = data.get('messages', [])
43
-
44
- if not messages:
45
- raise ValueError("No messages found in the request")
46
-
47
- # Prepare the payload for the PizzaGPT API
48
- # Assuming the user message is the last item in the 'messages' list
49
- user_message = messages[-1].get('content', '')
50
- if not user_message:
51
- raise ValueError("User message content is empty")
52
-
53
- payload = {
54
- "question": user_message # Pass the user message as 'question' to PizzaGPT
55
  }
 
 
 
56
 
57
- # Send the request to the PizzaGPT API
58
- response = requests.post(PIZZAGPT_API_URL, headers=headers, json=payload)
59
-
60
- # If the request is successful, return the response in OpenAI format
61
- if response.status_code == 200:
62
- pizza_response = response.json()
63
-
64
- # Log the response from PizzaGPT API
65
- app.logger.info(f"PizzaGPT API response: {json.dumps(pizza_response, indent=2)}")
66
-
67
- # Transform the PizzaGPT API response to OpenAI API response format
68
- openai_response = {
69
- "id": "unique-id-for-request", # You can generate a unique ID for each request
70
- "object": "chat.completion",
71
- "created": 1234567890, # Adjust this to match the timestamp if needed
72
- "model": "gpt-4o-mini", # This is the model you want to use in the response
73
- "choices": [
74
- {
75
- "message": {
76
- "role": "assistant", # Set the role to 'assistant' (PizzaGPT's reply)
77
- "content": pizza_response.get('response', '') # The message content from PizzaGPT
78
- },
79
- "finish_reason": "stop", # You can change this if needed
80
- "index": 0
81
- }
82
- ],
83
- "usage": {
84
- "total_tokens": len(pizza_response.get('response', '').split()) # Token count estimate
85
- }
86
- }
87
-
88
- return jsonify(openai_response)
89
- else:
90
- # Log the error if the response from PizzaGPT API is not 200
91
- app.logger.error(f"PizzaGPT API failed with status {response.status_code}: {response.text}")
92
- return jsonify({
93
- 'error': {
94
- 'message': f'Failed to fetch from PizzaGPT API: {response.text}',
95
- 'type': 'internal_error'
96
- }
97
- }), 500
98
-
99
- except Exception as e:
100
- # Catch any other errors and log them
101
- app.logger.error(f"Error in processing request: {str(e)}")
102
- return jsonify({
103
- 'error': {
104
- 'message': f'An error occurred: {str(e)}',
105
- 'type': 'internal_error'
106
- }
107
- }), 500
108
 
109
- if __name__ == '__main__':
110
- app.run(debug=True, host='0.0.0.0', port=5000)
 
 
 
 
 
 
1
  import requests
 
 
2
  import json
3
 
4
+ # Define the URL for the API
5
+ url = 'https://ap-northeast-2.apistage.ai/v1/web/demo/chat/completions'
6
 
7
+ # Define the headers (the same ones from the curl command)
 
 
 
 
 
 
8
  headers = {
9
+ 'accept': '*/*',
10
  'accept-language': 'en-US,en;q=0.9,ru;q=0.8',
11
  'content-type': 'application/json',
12
+ 'origin': 'https://console.upstage.ai',
 
13
  'priority': 'u=1, i',
14
+ 'referer': 'https://console.upstage.ai/',
15
  'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
16
  'sec-ch-ua-mobile': '?0',
17
  'sec-ch-ua-platform': '"Windows"',
18
  'sec-fetch-dest': 'empty',
19
  'sec-fetch-mode': 'cors',
20
+ 'sec-fetch-site': 'cross-site',
21
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
22
+ 'x-csrf-token': 'MTczMjAyMjY0MzI4OS5hMjM5OTFmZGRiYmQ1Yjk4ODFjY2Q0NDhlYTk0YTdiODg5OWYzZWY1NWFlOWQ2ZjEzOWU3ODkwNmMzM2I4MjM0'
23
  }
24
 
25
+ # Define the data (payload) to be sent in the POST request
26
+ data = {
27
+ 'stream': True,
28
+ 'messages': [
29
+ {
30
+ 'role': 'user',
31
+ 'content': 'Hi'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  }
33
+ ],
34
+ 'model': 'solar-pro'
35
+ }
36
 
37
+ # Send the POST request
38
+ response = requests.post(url, headers=headers, json=data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ # Check the status of the response and print the result
41
+ if response.status_code == 200:
42
+ print('Response received successfully:')
43
+ print(json.dumps(response.json(), indent=2))
44
+ else:
45
+ print(f'Failed to get a response. Status code: {response.status_code}')
46
+ print(response.text)