Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -87,8 +87,7 @@ claude_client = anthropic.Anthropic(api_key=YOUR_ANTHROPIC_TOKEN)
|
|
| 87 |
openai_client = openai.OpenAI(api_key=YOUR_OPENAI_TOKEN)
|
| 88 |
|
| 89 |
|
| 90 |
-
|
| 91 |
-
async def try_claude_api(system_message, claude_messages, timeout=5):
|
| 92 |
try:
|
| 93 |
start_time = time.time()
|
| 94 |
with claude_client.messages.stream(
|
|
@@ -98,22 +97,26 @@ async def try_claude_api(system_message, claude_messages, timeout=5):
|
|
| 98 |
messages=claude_messages
|
| 99 |
) as stream:
|
| 100 |
collected_content = ""
|
| 101 |
-
for chunk in stream:
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
raise TimeoutError("Claude API timeout")
|
| 104 |
if chunk.type == "content_block_delta":
|
| 105 |
collected_content += chunk.delta.text
|
| 106 |
yield collected_content
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
print(f"Claude API error: {str(e)}")
|
| 109 |
raise e
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
async def try_openai_api(openai_messages):
|
| 114 |
try:
|
| 115 |
stream = openai_client.chat.completions.create(
|
| 116 |
-
model="gpt-4o",
|
| 117 |
messages=openai_messages,
|
| 118 |
stream=True,
|
| 119 |
max_tokens=4096,
|
|
@@ -130,6 +133,7 @@ async def try_openai_api(openai_messages):
|
|
| 130 |
print(f"OpenAI API error: {str(e)}")
|
| 131 |
raise e
|
| 132 |
|
|
|
|
| 133 |
async def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
| 134 |
if not query or query.strip() == '':
|
| 135 |
query = random.choice(DEMO_LIST)['description']
|
|
|
|
| 87 |
openai_client = openai.OpenAI(api_key=YOUR_OPENAI_TOKEN)
|
| 88 |
|
| 89 |
|
| 90 |
+
async def try_claude_api(system_message, claude_messages, timeout=15): # timeout을 15초로 증가
|
|
|
|
| 91 |
try:
|
| 92 |
start_time = time.time()
|
| 93 |
with claude_client.messages.stream(
|
|
|
|
| 97 |
messages=claude_messages
|
| 98 |
) as stream:
|
| 99 |
collected_content = ""
|
| 100 |
+
async for chunk in stream: # async for 사용
|
| 101 |
+
current_time = time.time()
|
| 102 |
+
if current_time - start_time > timeout:
|
| 103 |
+
print(f"Claude API response time: {current_time - start_time:.2f} seconds")
|
| 104 |
raise TimeoutError("Claude API timeout")
|
| 105 |
if chunk.type == "content_block_delta":
|
| 106 |
collected_content += chunk.delta.text
|
| 107 |
yield collected_content
|
| 108 |
+
|
| 109 |
+
# 각 청크마다 타임아웃 카운터 리셋
|
| 110 |
+
start_time = current_time
|
| 111 |
+
|
| 112 |
except Exception as e:
|
| 113 |
print(f"Claude API error: {str(e)}")
|
| 114 |
raise e
|
| 115 |
|
|
|
|
|
|
|
| 116 |
async def try_openai_api(openai_messages):
|
| 117 |
try:
|
| 118 |
stream = openai_client.chat.completions.create(
|
| 119 |
+
model="gpt-4o", # 모델명 유지
|
| 120 |
messages=openai_messages,
|
| 121 |
stream=True,
|
| 122 |
max_tokens=4096,
|
|
|
|
| 133 |
print(f"OpenAI API error: {str(e)}")
|
| 134 |
raise e
|
| 135 |
|
| 136 |
+
|
| 137 |
async def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
| 138 |
if not query or query.strip() == '':
|
| 139 |
query = random.choice(DEMO_LIST)['description']
|