Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,10 @@ import time
|
|
| 5 |
import threading
|
| 6 |
import queue
|
| 7 |
|
| 8 |
-
def
|
| 9 |
api_key = os.environ.get("BASETEN_API_KEY")
|
| 10 |
if not api_key:
|
| 11 |
-
|
| 12 |
-
return
|
| 13 |
|
| 14 |
url = "https://model-4w56xy7q.api.baseten.co/development/predict"
|
| 15 |
|
|
@@ -24,8 +23,7 @@ def generate_email_async(company, recipient_name, product, progress=gr.Progress(
|
|
| 24 |
"product_name": product
|
| 25 |
}
|
| 26 |
|
| 27 |
-
|
| 28 |
-
progress(0.1)
|
| 29 |
|
| 30 |
def make_request(result_queue):
|
| 31 |
try:
|
|
@@ -49,24 +47,22 @@ def generate_email_async(company, recipient_name, product, progress=gr.Progress(
|
|
| 49 |
while thread.is_alive():
|
| 50 |
elapsed_time = time.time() - start_time
|
| 51 |
if elapsed_time > 300: # 5 minutes timeout
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
yield f"Still working... (Elapsed time: {elapsed_time:.0f} seconds)"
|
| 55 |
-
progress(min(0.1 + (elapsed_time / 300) * 0.8, 0.9)) # Progress up to 90%
|
| 56 |
time.sleep(5) # Update every 5 seconds
|
| 57 |
|
| 58 |
thread.join()
|
| 59 |
status, result = result_queue.get()
|
| 60 |
|
| 61 |
if status == "success":
|
| 62 |
-
progress(1.0)
|
| 63 |
-
|
| 64 |
else:
|
| 65 |
-
|
| 66 |
|
| 67 |
# Create the Gradio interface
|
| 68 |
iface = gr.Interface(
|
| 69 |
-
fn=
|
| 70 |
inputs=[
|
| 71 |
gr.Textbox(label="Company Name"),
|
| 72 |
gr.Textbox(label="Recipient Name"),
|
|
@@ -75,8 +71,7 @@ iface = gr.Interface(
|
|
| 75 |
outputs=gr.Textbox(label="Generated Email"),
|
| 76 |
title="AI Email Generator",
|
| 77 |
description="Generate personalized emails using AI (This process may take several minutes)",
|
| 78 |
-
allow_flagging="never"
|
| 79 |
-
live=True
|
| 80 |
)
|
| 81 |
|
| 82 |
# Launch the interface
|
|
|
|
| 5 |
import threading
|
| 6 |
import queue
|
| 7 |
|
| 8 |
+
def generate_email(company, recipient_name, product, progress=gr.Progress()):
|
| 9 |
api_key = os.environ.get("BASETEN_API_KEY")
|
| 10 |
if not api_key:
|
| 11 |
+
return "Error: API_KEY not found in environment variables"
|
|
|
|
| 12 |
|
| 13 |
url = "https://model-4w56xy7q.api.baseten.co/development/predict"
|
| 14 |
|
|
|
|
| 23 |
"product_name": product
|
| 24 |
}
|
| 25 |
|
| 26 |
+
progress(0.1, "Initiating email generation...")
|
|
|
|
| 27 |
|
| 28 |
def make_request(result_queue):
|
| 29 |
try:
|
|
|
|
| 47 |
while thread.is_alive():
|
| 48 |
elapsed_time = time.time() - start_time
|
| 49 |
if elapsed_time > 300: # 5 minutes timeout
|
| 50 |
+
return "Error: Process took too long. Please try again later."
|
| 51 |
+
progress(min(0.1 + (elapsed_time / 300) * 0.8, 0.9), f"Generating email... (Elapsed time: {elapsed_time:.0f} seconds)")
|
|
|
|
|
|
|
| 52 |
time.sleep(5) # Update every 5 seconds
|
| 53 |
|
| 54 |
thread.join()
|
| 55 |
status, result = result_queue.get()
|
| 56 |
|
| 57 |
if status == "success":
|
| 58 |
+
progress(1.0, "Email generated successfully!")
|
| 59 |
+
return result
|
| 60 |
else:
|
| 61 |
+
return result
|
| 62 |
|
| 63 |
# Create the Gradio interface
|
| 64 |
iface = gr.Interface(
|
| 65 |
+
fn=generate_email,
|
| 66 |
inputs=[
|
| 67 |
gr.Textbox(label="Company Name"),
|
| 68 |
gr.Textbox(label="Recipient Name"),
|
|
|
|
| 71 |
outputs=gr.Textbox(label="Generated Email"),
|
| 72 |
title="AI Email Generator",
|
| 73 |
description="Generate personalized emails using AI (This process may take several minutes)",
|
| 74 |
+
allow_flagging="never"
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
# Launch the interface
|