Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,143 +0,0 @@
|
|
| 1 |
-
using System;
|
| 2 |
-
using System.Collections;
|
| 3 |
-
using System.Collections.Generic;
|
| 4 |
-
using System.Text;
|
| 5 |
-
using UnityEngine;
|
| 6 |
-
using UnityEngine.Networking;
|
| 7 |
-
|
| 8 |
-
public class TradingGameAIApiClient : MonoBehaviour
|
| 9 |
-
{
|
| 10 |
-
[Header("API")]
|
| 11 |
-
[SerializeField] private string apiUrl = "https://j-js-tradinggameai.hf.space/chat";
|
| 12 |
-
|
| 13 |
-
[Header("Debug")]
|
| 14 |
-
[SerializeField] private bool logRequests = true;
|
| 15 |
-
[SerializeField] private bool logResponses = true;
|
| 16 |
-
|
| 17 |
-
[Serializable]
|
| 18 |
-
public class ChatMessage
|
| 19 |
-
{
|
| 20 |
-
public string role;
|
| 21 |
-
public string text;
|
| 22 |
-
|
| 23 |
-
public ChatMessage(string role, string text)
|
| 24 |
-
{
|
| 25 |
-
this.role = role;
|
| 26 |
-
this.text = text;
|
| 27 |
-
}
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
[Serializable]
|
| 31 |
-
public class ChatRequest
|
| 32 |
-
{
|
| 33 |
-
public string message;
|
| 34 |
-
public List<ChatMessage> chat_history;
|
| 35 |
-
public float tone;
|
| 36 |
-
public float verbosity;
|
| 37 |
-
public float transparency;
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
[Serializable]
|
| 41 |
-
public class ChatResponse
|
| 42 |
-
{
|
| 43 |
-
public string reply;
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
public void SendChatRequest(
|
| 47 |
-
string message,
|
| 48 |
-
List<ChatMessage> chatHistory,
|
| 49 |
-
float tone,
|
| 50 |
-
float verbosity,
|
| 51 |
-
float transparency,
|
| 52 |
-
Action<string> onSuccess,
|
| 53 |
-
Action<string> onError)
|
| 54 |
-
{
|
| 55 |
-
StartCoroutine(SendChatRequestRoutine(
|
| 56 |
-
message,
|
| 57 |
-
chatHistory,
|
| 58 |
-
tone,
|
| 59 |
-
verbosity,
|
| 60 |
-
transparency,
|
| 61 |
-
onSuccess,
|
| 62 |
-
onError
|
| 63 |
-
));
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
private IEnumerator SendChatRequestRoutine(
|
| 67 |
-
string message,
|
| 68 |
-
List<ChatMessage> chatHistory,
|
| 69 |
-
float tone,
|
| 70 |
-
float verbosity,
|
| 71 |
-
float transparency,
|
| 72 |
-
Action<string> onSuccess,
|
| 73 |
-
Action<string> onError)
|
| 74 |
-
{
|
| 75 |
-
Debug.Log("TradingGameAI apiUrl = " + apiUrl);
|
| 76 |
-
|
| 77 |
-
ChatRequest requestData = new ChatRequest
|
| 78 |
-
{
|
| 79 |
-
message = message,
|
| 80 |
-
chat_history = chatHistory ?? new List<ChatMessage>(),
|
| 81 |
-
tone = tone,
|
| 82 |
-
verbosity = verbosity,
|
| 83 |
-
transparency = transparency
|
| 84 |
-
};
|
| 85 |
-
|
| 86 |
-
string json = JsonUtility.ToJson(requestData);
|
| 87 |
-
|
| 88 |
-
if (logRequests)
|
| 89 |
-
Debug.Log("TradingGameAI Request JSON: " + json);
|
| 90 |
-
|
| 91 |
-
using (UnityWebRequest request = new UnityWebRequest(apiUrl, "POST"))
|
| 92 |
-
{
|
| 93 |
-
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
|
| 94 |
-
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
| 95 |
-
request.downloadHandler = new DownloadHandlerBuffer();
|
| 96 |
-
request.SetRequestHeader("Content-Type", "application/json");
|
| 97 |
-
request.SetRequestHeader("Accept", "application/json");
|
| 98 |
-
|
| 99 |
-
yield return request.SendWebRequest();
|
| 100 |
-
|
| 101 |
-
string responseText = request.downloadHandler.text;
|
| 102 |
-
|
| 103 |
-
if (logResponses)
|
| 104 |
-
Debug.Log("TradingGameAI Raw Response: " + responseText);
|
| 105 |
-
|
| 106 |
-
if (request.result != UnityWebRequest.Result.Success)
|
| 107 |
-
{
|
| 108 |
-
string errorMessage = "API request failed: " + request.error;
|
| 109 |
-
|
| 110 |
-
if (!string.IsNullOrEmpty(responseText))
|
| 111 |
-
errorMessage += "\nServer: " + responseText;
|
| 112 |
-
|
| 113 |
-
Debug.LogError(errorMessage);
|
| 114 |
-
onError?.Invoke(errorMessage);
|
| 115 |
-
yield break;
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
ChatResponse responseData = null;
|
| 119 |
-
|
| 120 |
-
try
|
| 121 |
-
{
|
| 122 |
-
responseData = JsonUtility.FromJson<ChatResponse>(responseText);
|
| 123 |
-
}
|
| 124 |
-
catch (Exception e)
|
| 125 |
-
{
|
| 126 |
-
string parseError = "Failed to parse API response: " + e.Message + "\nResponse: " + responseText;
|
| 127 |
-
Debug.LogError(parseError);
|
| 128 |
-
onError?.Invoke(parseError);
|
| 129 |
-
yield break;
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
if (responseData == null || string.IsNullOrEmpty(responseData.reply))
|
| 133 |
-
{
|
| 134 |
-
string invalidResponse = "API returned empty or invalid reply.\nResponse: " + responseText;
|
| 135 |
-
Debug.LogError(invalidResponse);
|
| 136 |
-
onError?.Invoke(invalidResponse);
|
| 137 |
-
yield break;
|
| 138 |
-
}
|
| 139 |
-
|
| 140 |
-
onSuccess?.Invoke(responseData.reply);
|
| 141 |
-
}
|
| 142 |
-
}
|
| 143 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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