Update app.py
Browse files
app.py
CHANGED
@@ -1632,8 +1632,10 @@ print("Transformer Output:", transformer_output)
|
|
1632 |
|
1633 |
|
1634 |
|
1635 |
-
import gradio as gr
|
1636 |
-
|
|
|
|
|
1637 |
|
1638 |
# Load system prompt from environment variable
|
1639 |
SYSTEM_PROMPT = os.getenv("SYSTEM_PROMPT")
|
@@ -1646,23 +1648,43 @@ client = OpenAI(
|
|
1646 |
api_key="hyperbolic"
|
1647 |
)
|
1648 |
|
1649 |
-
|
|
|
|
|
|
|
|
|
1650 |
# If history is empty, insert the system prompt
|
1651 |
if not any(msg["role"] == "system" for msg in history):
|
1652 |
history.insert(0, {"role": "system", "content": SYSTEM_PROMPT})
|
1653 |
|
1654 |
-
history.append({"role": "user", "content": message})
|
1655 |
-
|
1656 |
-
|
1657 |
-
|
1658 |
-
|
1659 |
-
|
1660 |
-
|
1661 |
-
|
1662 |
-
|
1663 |
-
|
1664 |
-
|
1665 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1666 |
|
1667 |
demo = gr.ChatInterface(
|
1668 |
fn=predict,
|
|
|
1632 |
|
1633 |
|
1634 |
|
1635 |
+
import gradio as gr
|
1636 |
+
import os
|
1637 |
+
import time
|
1638 |
+
from openai import OpenAI
|
1639 |
|
1640 |
# Load system prompt from environment variable
|
1641 |
SYSTEM_PROMPT = os.getenv("SYSTEM_PROMPT")
|
|
|
1648 |
api_key="hyperbolic"
|
1649 |
)
|
1650 |
|
1651 |
+
# Retry settings
|
1652 |
+
MAX_RETRIES = 5 # Number of retries
|
1653 |
+
RETRY_DELAY = 1 # Delay in seconds between retries
|
1654 |
+
|
1655 |
+
def predict(message, history):
|
1656 |
# If history is empty, insert the system prompt
|
1657 |
if not any(msg["role"] == "system" for msg in history):
|
1658 |
history.insert(0, {"role": "system", "content": SYSTEM_PROMPT})
|
1659 |
|
1660 |
+
history.append({"role": "user", "content": message})
|
1661 |
+
|
1662 |
+
retries = 0
|
1663 |
+
while retries < MAX_RETRIES:
|
1664 |
+
try:
|
1665 |
+
# Attempt to get the completion stream
|
1666 |
+
stream = client.chat.completions.create(
|
1667 |
+
messages=history,
|
1668 |
+
model=os.getenv("ACCEMULECTPLUS"),
|
1669 |
+
stream=True
|
1670 |
+
)
|
1671 |
+
|
1672 |
+
# Collect the response
|
1673 |
+
chunks = []
|
1674 |
+
for chunk in stream:
|
1675 |
+
chunks.append(chunk.choices[0].delta.content or "")
|
1676 |
+
yield "".join(chunks)
|
1677 |
+
|
1678 |
+
# If no exception occurs, break the retry loop
|
1679 |
+
break
|
1680 |
+
except Exception as e:
|
1681 |
+
print(f"Error occurred: {e}. Retrying ({retries + 1}/{MAX_RETRIES})...")
|
1682 |
+
retries += 1
|
1683 |
+
if retries < MAX_RETRIES:
|
1684 |
+
time.sleep(RETRY_DELAY) # Delay before retrying
|
1685 |
+
else:
|
1686 |
+
print("Max retries reached. Could not complete the request.")
|
1687 |
+
yield "Sorry, there was an error processing your request. Please try again later."
|
1688 |
|
1689 |
demo = gr.ChatInterface(
|
1690 |
fn=predict,
|