openfree commited on
Commit
75e8c43
Β·
verified Β·
1 Parent(s): baa1dde

Update app-backup.py

Browse files
Files changed (1) hide show
  1. app-backup.py +52 -49
app-backup.py CHANGED
@@ -12,11 +12,11 @@ from typing import List, Dict, Optional, Any
12
  import logging
13
  import uuid
14
 
15
- # λͺ¨λ“ˆμ΄ μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ ν˜„μž¬ λ””λ ‰ν† λ¦¬μ—μ„œ κ°€μ Έμ˜€λ„λ‘ μ‹œλ„
16
  try:
17
  from recursive_thinking_ai import EnhancedRecursiveThinkingChat
18
  except ModuleNotFoundError:
19
- # ν˜„μž¬ 디렉토리에 recursive_thinking_ai.py 파일이 μžˆμ–΄μ•Ό 함
20
  import sys
21
  sys.path.append('.')
22
  from recursive_thinking_ai import EnhancedRecursiveThinkingChat
@@ -29,8 +29,8 @@ logging.basicConfig(
29
  logger = logging.getLogger(__name__)
30
 
31
  app = FastAPI(
32
- title="Chain of Recursive Thoughts",
33
- description="API for Enhanced Recursive Thinking Chat",
34
  version="1.0.0"
35
  )
36
 
@@ -46,14 +46,14 @@ app.add_middleware(
46
  # Create a dictionary to store chat instances
47
  chat_instances = {}
48
 
49
- # ν™˜κ²½ λ³€μˆ˜μ—μ„œ API ν‚€ κ°€μ Έμ˜€κΈ°
50
  API_KEY = os.getenv("OPENROUTE_API")
51
  if not API_KEY:
52
- logger.warning("OPENROUTE_API ν™˜κ²½ λ³€μˆ˜κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. 일뢀 κΈ°λŠ₯이 λ™μž‘ν•˜μ§€ μ•Šμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.")
53
 
54
  # Pydantic models for request/response validation
55
  class ChatConfig(BaseModel):
56
- # api_key ν•­λͺ©μ€ μ œκ±°ν•˜κ³ , λͺ¨λΈκ³Ό temperature만 받도둝 μˆ˜μ •
57
  model: str = "mistralai/mistral-small-3.1-24b-instruct:free"
58
  temperature: Optional[float] = Field(default=0.7, ge=0.0, le=1.0)
59
 
@@ -82,7 +82,7 @@ class InitResponse(BaseModel):
82
  session_id: str
83
  status: str
84
 
85
- # κ°„λ‹¨ν•œ HTML μΈν„°νŽ˜μ΄μŠ€ 제곡 (API ν‚€ μž…λ ₯ 폼을 μ œκ±°ν•¨)
86
  @app.get("/", response_class=HTMLResponse)
87
  async def root():
88
  """Root endpoint with a simple HTML interface"""
@@ -90,7 +90,7 @@ async def root():
90
  <!DOCTYPE html>
91
  <html>
92
  <head>
93
- <title>RecThink API</title>
94
  <style>
95
  body {{
96
  font-family: Arial, sans-serif;
@@ -149,48 +149,53 @@ async def root():
149
  </style>
150
  </head>
151
  <body>
152
- <h1>RecThink API μΈν„°νŽ˜μ΄μŠ€</h1>
153
  <div class="container">
154
  <div id="init-form">
155
- <h2>1. μ±„νŒ… μ΄ˆκΈ°ν™”</h2>
156
 
157
- <!-- API ν‚€ μž…λ ₯λž€ 제거 -->
158
- <label for="model">λͺ¨λΈ:</label>
159
  <input type="text" id="model" value="mistralai/mistral-small-3.1-24b-instruct:free">
160
 
161
- <label for="temperature">μ˜¨λ„ (Temperature):</label>
162
  <input type="number" id="temperature" min="0" max="1" step="0.1" value="0.7">
163
 
164
- <button onclick="initializeChat()">μ΄ˆκΈ°ν™”</button>
165
  </div>
166
 
167
  <div id="chat-form" style="display: none;">
168
- <h2>2. λ©”μ‹œμ§€ 전솑</h2>
169
- <p>μ„Έμ…˜ ID: <span id="session-id"></span></p>
170
 
171
- <label for="message">λ©”μ‹œμ§€:</label>
172
- <textarea id="message" rows="4" placeholder="λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”"></textarea>
173
 
174
- <label for="thinking-rounds">사고 λΌμš΄λ“œ (선택사항):</label>
175
- <input type="number" id="thinking-rounds" min="1" max="10" placeholder="μžλ™ κ²°μ •">
176
 
177
- <label for="alternatives">λŒ€μ•ˆ 개수 (선택사항):</label>
178
  <input type="number" id="alternatives" min="1" max="5" value="3">
179
 
180
- <button onclick="sendMessage()">전솑</button>
181
- <button onclick="resetChat()" style="background-color: #f44336;">μ΄ˆκΈ°ν™”</button>
182
  </div>
183
 
184
  <div id="response-container" style="display: none;">
185
- <h2>3. 응닡</h2>
186
- <div id="response">응닡이 여기에 ν‘œμ‹œλ©λ‹ˆλ‹€...</div>
187
  <div class="log">
188
- <h3>생각 κ³Όμ • 둜그:</h3>
189
  <div id="thinking-log"></div>
190
  </div>
191
  </div>
192
  </div>
193
 
 
 
 
 
 
194
  <script>
195
  let currentSessionId = null;
196
 
@@ -219,16 +224,16 @@ async def root():
219
  document.getElementById('chat-form').style.display = 'block';
220
  document.getElementById('response-container').style.display = 'block';
221
  }} else {{
222
- alert('μ΄ˆκΈ°ν™” μ‹€νŒ¨: ' + (data.detail || 'μ•Œ 수 μ—†λŠ” 였λ₯˜'));
223
  }}
224
  }} catch (error) {{
225
- alert('였λ₯˜ λ°œμƒ: ' + error.message);
226
  }}
227
  }}
228
 
229
  async function sendMessage() {{
230
  if (!currentSessionId) {{
231
- alert('λ¨Όμ € μ±„νŒ…μ„ μ΄ˆκΈ°ν™”ν•΄μ£Όμ„Έμš”');
232
  return;
233
  }}
234
 
@@ -237,11 +242,11 @@ async def root():
237
  const alternatives = document.getElementById('alternatives').value;
238
 
239
  if (!message) {{
240
- alert('λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”');
241
  return;
242
  }}
243
 
244
- document.getElementById('response').textContent = '처리 쀑...';
245
  document.getElementById('thinking-log').textContent = '';
246
 
247
  try {{
@@ -266,22 +271,21 @@ async def root():
266
  // Display thinking history
267
  let thinkingLogHTML = '';
268
  data.thinking_history.forEach(item => {{
269
- const selected = item.selected ? ' βœ“ 선택됨' : '';
270
- // μˆ˜μ •λœ λΆ€λΆ„: λ¬Έμžμ—΄ μ—°κ²° λ°©μ‹μœΌλ‘œ μ „ν™˜
271
- thinkingLogHTML += "<p><strong>λΌμš΄λ“œ " + item.round + selected + ":</strong> ";
272
 
273
  if (item.explanation && item.selected) {{
274
- thinkingLogHTML += "<br><em>선택 이유: " + item.explanation + "</em>";
275
  }}
276
  thinkingLogHTML += "</p>";
277
  }});
278
 
279
  document.getElementById('thinking-log').innerHTML = thinkingLogHTML;
280
  }} else {{
281
- document.getElementById('response').textContent = '였λ₯˜: ' + (data.detail || 'μ•Œ 수 μ—†λŠ” 였λ₯˜');
282
  }}
283
  }} catch (error) {{
284
- document.getElementById('response').textContent = '였λ₯˜ λ°œμƒ: ' + error.message;
285
  }}
286
  }}
287
 
@@ -300,7 +304,7 @@ async def root():
300
  """
301
  return html_content
302
 
303
- # Healthcheck endpoint
304
  @app.get("/health")
305
  async def health_check():
306
  """Health check endpoint"""
@@ -308,14 +312,14 @@ async def health_check():
308
 
309
  @app.post("/api/initialize", response_model=InitResponse)
310
  async def initialize_chat(config: ChatConfig):
311
- """Initialize a new chat session using environment API key"""
312
  try:
313
  # Generate a session ID
314
  session_id = f"session_{datetime.now().strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}"
315
 
316
- # ν™˜κ²½ λ³€μˆ˜κ°€ 없을 경우 μ—λŸ¬ 처리(λ˜λŠ” κ²½κ³ )
317
  if not API_KEY:
318
- raise HTTPException(status_code=400, detail="OPENROUTE_API ν™˜κ²½ λ³€μˆ˜κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.")
319
 
320
  # Initialize the chat instance
321
  chat = EnhancedRecursiveThinkingChat(
@@ -336,7 +340,7 @@ async def initialize_chat(config: ChatConfig):
336
 
337
  @app.post("/api/send_message")
338
  async def send_message(request: MessageRequest):
339
- """Send a message and get a response with thinking process"""
340
  try:
341
  if request.session_id not in chat_instances:
342
  raise HTTPException(status_code=404, detail="Session not found")
@@ -387,7 +391,7 @@ async def send_message(request: MessageRequest):
387
 
388
  @app.post("/api/save")
389
  async def save_conversation(request: SaveRequest):
390
- """Save the conversation or full thinking log"""
391
  try:
392
  if request.session_id not in chat_instances:
393
  raise HTTPException(status_code=404, detail="Session not found")
@@ -486,7 +490,7 @@ class ConnectionManager:
486
 
487
  manager = ConnectionManager()
488
 
489
- # WebSocket for streaming thinking process
490
  @app.websocket("/ws/{session_id}")
491
  async def websocket_endpoint(websocket: WebSocket, session_id: str):
492
  try:
@@ -499,7 +503,7 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
499
 
500
  chat = chat_instances[session_id]["chat"]
501
 
502
- # Set up a custom callback to stream thinking process
503
  original_call_api = chat._call_api
504
 
505
  async def stream_callback(chunk):
@@ -592,16 +596,15 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
592
  except:
593
  pass
594
  finally:
595
- # Restore original method if needed
596
  if 'chat' in locals() and 'original_call_api' in locals():
597
  chat._call_api = original_call_api
598
 
599
  # Make sure to disconnect
600
  manager.disconnect(session_id)
601
 
602
- # 포트 μ„€μ • - ν—ˆκΉ…νŽ˜μ΄μŠ€ μŠ€νŽ˜μ΄μŠ€μ—μ„œλŠ” 7860 포트λ₯Ό μ‚¬μš©ν•΄μ•Ό 함
603
  if __name__ == "__main__":
604
- # ν—ˆκΉ…νŽ˜μ΄μŠ€ μŠ€νŽ˜μ΄μŠ€μ—μ„œ μ‹€ν–‰μ‹œ 포트 7860 μ‚¬μš©
605
  port = 7860
606
  print(f"Starting server on port {port}")
607
  uvicorn.run("app:app", host="0.0.0.0", port=port)
 
12
  import logging
13
  import uuid
14
 
15
+ # If the module does not exist, try to import from the current directory
16
  try:
17
  from recursive_thinking_ai import EnhancedRecursiveThinkingChat
18
  except ModuleNotFoundError:
19
+ # The file recursive_thinking_ai.py must exist in the current directory
20
  import sys
21
  sys.path.append('.')
22
  from recursive_thinking_ai import EnhancedRecursiveThinkingChat
 
29
  logger = logging.getLogger(__name__)
30
 
31
  app = FastAPI(
32
+ title="Chain-of-Recursive-Thoughts: TEST",
33
+ description="https://github.com/PhialsBasement/Chain-of-Recursive-Thoughts",
34
  version="1.0.0"
35
  )
36
 
 
46
  # Create a dictionary to store chat instances
47
  chat_instances = {}
48
 
49
+ # Retrieve API key from environment variable
50
  API_KEY = os.getenv("OPENROUTE_API")
51
  if not API_KEY:
52
+ logger.warning("The OPENROUTE_API environment variable is not set. Some features may not work.")
53
 
54
  # Pydantic models for request/response validation
55
  class ChatConfig(BaseModel):
56
+ # Removed api_key field; only model and temperature are received
57
  model: str = "mistralai/mistral-small-3.1-24b-instruct:free"
58
  temperature: Optional[float] = Field(default=0.7, ge=0.0, le=1.0)
59
 
 
82
  session_id: str
83
  status: str
84
 
85
+ # Simple HTML interface (API key input form removed)
86
  @app.get("/", response_class=HTMLResponse)
87
  async def root():
88
  """Root endpoint with a simple HTML interface"""
 
90
  <!DOCTYPE html>
91
  <html>
92
  <head>
93
+ <title>Chain-of-Recursive-Thoughts: TEST</title>
94
  <style>
95
  body {{
96
  font-family: Arial, sans-serif;
 
149
  </style>
150
  </head>
151
  <body>
152
+ <h1>Chain-of-Recursive-Thoughts: TEST</h1>
153
  <div class="container">
154
  <div id="init-form">
155
+ <h2>1. Initialize Chat</h2>
156
 
157
+ <!-- API key input removed -->
158
+ <label for="model">Model:</label>
159
  <input type="text" id="model" value="mistralai/mistral-small-3.1-24b-instruct:free">
160
 
161
+ <label for="temperature">Temperature:</label>
162
  <input type="number" id="temperature" min="0" max="1" step="0.1" value="0.7">
163
 
164
+ <button onclick="initializeChat()">Initialize</button>
165
  </div>
166
 
167
  <div id="chat-form" style="display: none;">
168
+ <h2>2. Send Message</h2>
169
+ <p>Session ID: <span id="session-id"></span></p>
170
 
171
+ <label for="message">Message:</label>
172
+ <textarea id="message" rows="4" placeholder="Enter your message"></textarea>
173
 
174
+ <label for="thinking-rounds">Thinking Rounds (optional):</label>
175
+ <input type="number" id="thinking-rounds" min="1" max="10" placeholder="Auto">
176
 
177
+ <label for="alternatives">Number of Alternatives (optional):</label>
178
  <input type="number" id="alternatives" min="1" max="5" value="3">
179
 
180
+ <button onclick="sendMessage()">Send</button>
181
+ <button onclick="resetChat()" style="background-color: #f44336;">Reset</button>
182
  </div>
183
 
184
  <div id="response-container" style="display: none;">
185
+ <h2>3. Response</h2>
186
+ <div id="response">The response will appear here...</div>
187
  <div class="log">
188
+ <h3>Thinking Process Log:</h3>
189
  <div id="thinking-log"></div>
190
  </div>
191
  </div>
192
  </div>
193
 
194
+ <div style="margin-top: 30px;">
195
+ <p>Repo: https://github.com/PhialsBasement/Chain-of-Recursive-Thoughts</p>
196
+ <p>Community: https://discord.gg/openfreeai</p>
197
+ </div>
198
+
199
  <script>
200
  let currentSessionId = null;
201
 
 
224
  document.getElementById('chat-form').style.display = 'block';
225
  document.getElementById('response-container').style.display = 'block';
226
  }} else {{
227
+ alert('Initialization failed: ' + (data.detail || 'Unknown error'));
228
  }}
229
  }} catch (error) {{
230
+ alert('An error occurred: ' + error.message);
231
  }}
232
  }}
233
 
234
  async function sendMessage() {{
235
  if (!currentSessionId) {{
236
+ alert('Please initialize a chat session first.');
237
  return;
238
  }}
239
 
 
242
  const alternatives = document.getElementById('alternatives').value;
243
 
244
  if (!message) {{
245
+ alert('Please enter a message.');
246
  return;
247
  }}
248
 
249
+ document.getElementById('response').textContent = 'Processing...';
250
  document.getElementById('thinking-log').textContent = '';
251
 
252
  try {{
 
271
  // Display thinking history
272
  let thinkingLogHTML = '';
273
  data.thinking_history.forEach(item => {{
274
+ const selected = item.selected ? ' βœ“ Selected' : '';
275
+ thinkingLogHTML += "<p><strong>Round " + item.round + selected + ":</strong> ";
 
276
 
277
  if (item.explanation && item.selected) {{
278
+ thinkingLogHTML += "<br><em>Reason for selection: " + item.explanation + "</em>";
279
  }}
280
  thinkingLogHTML += "</p>";
281
  }});
282
 
283
  document.getElementById('thinking-log').innerHTML = thinkingLogHTML;
284
  }} else {{
285
+ document.getElementById('response').textContent = 'Error: ' + (data.detail || 'Unknown error');
286
  }}
287
  }} catch (error) {{
288
+ document.getElementById('response').textContent = 'An error occurred: ' + error.message;
289
  }}
290
  }}
291
 
 
304
  """
305
  return html_content
306
 
307
+ # Health check endpoint
308
  @app.get("/health")
309
  async def health_check():
310
  """Health check endpoint"""
 
312
 
313
  @app.post("/api/initialize", response_model=InitResponse)
314
  async def initialize_chat(config: ChatConfig):
315
+ """Initialize a new chat session using the environment API key"""
316
  try:
317
  # Generate a session ID
318
  session_id = f"session_{datetime.now().strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}"
319
 
320
+ # If the environment variable is missing, raise an error (or warning)
321
  if not API_KEY:
322
+ raise HTTPException(status_code=400, detail="The OPENROUTE_API environment variable is not set.")
323
 
324
  # Initialize the chat instance
325
  chat = EnhancedRecursiveThinkingChat(
 
340
 
341
  @app.post("/api/send_message")
342
  async def send_message(request: MessageRequest):
343
+ """Send a message and get a response with the thinking process"""
344
  try:
345
  if request.session_id not in chat_instances:
346
  raise HTTPException(status_code=404, detail="Session not found")
 
391
 
392
  @app.post("/api/save")
393
  async def save_conversation(request: SaveRequest):
394
+ """Save the conversation or the full thinking log"""
395
  try:
396
  if request.session_id not in chat_instances:
397
  raise HTTPException(status_code=404, detail="Session not found")
 
490
 
491
  manager = ConnectionManager()
492
 
493
+ # WebSocket for streaming the thinking process
494
  @app.websocket("/ws/{session_id}")
495
  async def websocket_endpoint(websocket: WebSocket, session_id: str):
496
  try:
 
503
 
504
  chat = chat_instances[session_id]["chat"]
505
 
506
+ # Set up a custom callback to stream the thinking process
507
  original_call_api = chat._call_api
508
 
509
  async def stream_callback(chunk):
 
596
  except:
597
  pass
598
  finally:
599
+ # Restore the original method if needed
600
  if 'chat' in locals() and 'original_call_api' in locals():
601
  chat._call_api = original_call_api
602
 
603
  # Make sure to disconnect
604
  manager.disconnect(session_id)
605
 
606
+ # Use port 7860 for Hugging Face Spaces
607
  if __name__ == "__main__":
 
608
  port = 7860
609
  print(f"Starting server on port {port}")
610
  uvicorn.run("app:app", host="0.0.0.0", port=port)