Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import requests
|
|
5 |
import io
|
6 |
from PIL import Image
|
7 |
import os
|
8 |
-
import time
|
9 |
|
10 |
# Set up your OpenAI API key (make sure it's stored as an environment variable)
|
11 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
@@ -28,20 +28,20 @@ else:
|
|
28 |
|
29 |
API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
|
30 |
|
31 |
-
# Define the OpenAI
|
32 |
-
def generate_with_gpt3(prompt
|
33 |
try:
|
34 |
-
print("Generating text with
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
40 |
top_p=0.9,
|
41 |
-
frequency_penalty=0.0,
|
42 |
-
presence_penalty=0.0
|
43 |
)
|
44 |
-
generated_text = response
|
45 |
print("Text generation completed.")
|
46 |
return generated_text
|
47 |
except Exception as e:
|
@@ -61,20 +61,18 @@ def translate_and_generate_image(tamil_text):
|
|
61 |
except Exception as e:
|
62 |
return "Error during translation: " + str(e), "", None
|
63 |
|
64 |
-
#
|
65 |
-
time.sleep(1) # Optional: Add a small delay to ensure proper execution order
|
66 |
|
67 |
-
# Step 2: Generate high-quality descriptive text using OpenAI's
|
68 |
try:
|
69 |
print("Generating descriptive text from translated English text...")
|
70 |
prompt = f"Create a detailed and creative description based on the following text: {translated_text}"
|
71 |
-
generated_text = generate_with_gpt3(prompt
|
72 |
print(f"Text generation completed: {generated_text}")
|
73 |
except Exception as e:
|
74 |
return translated_text, f"Error during text generation: {e}", None
|
75 |
|
76 |
-
#
|
77 |
-
time.sleep(1) # Optional: Add a small delay to ensure proper execution order
|
78 |
|
79 |
# Step 3: Use the generated English text to create an image
|
80 |
try:
|
@@ -101,7 +99,7 @@ iface = gr.Interface(
|
|
101 |
gr.Textbox(label="Generated Descriptive Text"),
|
102 |
gr.Image(label="Generated Image")],
|
103 |
title="Tamil to English Translation, GPT-3 Text Generation, and Image Creation",
|
104 |
-
description="Translate Tamil text to English using Facebook's mbart-large-50 model, generate high-quality text using GPT-3, and create an image using the generated text.",
|
105 |
)
|
106 |
|
107 |
# Launch Gradio app without `share=True`
|
|
|
5 |
import io
|
6 |
from PIL import Image
|
7 |
import os
|
8 |
+
import time
|
9 |
|
10 |
# Set up your OpenAI API key (make sure it's stored as an environment variable)
|
11 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
28 |
|
29 |
API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
|
30 |
|
31 |
+
# Define the OpenAI ChatCompletion function using `gpt-3.5-turbo`
|
32 |
+
def generate_with_gpt3(prompt):
|
33 |
try:
|
34 |
+
print("Generating text with OpenAI ChatCompletion...")
|
35 |
+
# Use ChatCompletion with gpt-3.5-turbo
|
36 |
+
response = openai.ChatCompletion.create(
|
37 |
+
model="gpt-3.5-turbo", # Use "gpt-4" if you have access
|
38 |
+
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
39 |
+
{"role": "user", "content": prompt}],
|
40 |
+
max_tokens=150,
|
41 |
+
temperature=0.2,
|
42 |
top_p=0.9,
|
|
|
|
|
43 |
)
|
44 |
+
generated_text = response['choices'][0]['message']['content'].strip()
|
45 |
print("Text generation completed.")
|
46 |
return generated_text
|
47 |
except Exception as e:
|
|
|
61 |
except Exception as e:
|
62 |
return "Error during translation: " + str(e), "", None
|
63 |
|
64 |
+
time.sleep(1) # Optional: Small delay to ensure sequential execution
|
|
|
65 |
|
66 |
+
# Step 2: Generate high-quality descriptive text using OpenAI's ChatCompletion
|
67 |
try:
|
68 |
print("Generating descriptive text from translated English text...")
|
69 |
prompt = f"Create a detailed and creative description based on the following text: {translated_text}"
|
70 |
+
generated_text = generate_with_gpt3(prompt)
|
71 |
print(f"Text generation completed: {generated_text}")
|
72 |
except Exception as e:
|
73 |
return translated_text, f"Error during text generation: {e}", None
|
74 |
|
75 |
+
time.sleep(1) # Optional: Small delay to ensure sequential execution
|
|
|
76 |
|
77 |
# Step 3: Use the generated English text to create an image
|
78 |
try:
|
|
|
99 |
gr.Textbox(label="Generated Descriptive Text"),
|
100 |
gr.Image(label="Generated Image")],
|
101 |
title="Tamil to English Translation, GPT-3 Text Generation, and Image Creation",
|
102 |
+
description="Translate Tamil text to English using Facebook's mbart-large-50 model, generate high-quality text using GPT-3.5-turbo, and create an image using the generated text.",
|
103 |
)
|
104 |
|
105 |
# Launch Gradio app without `share=True`
|