gokilashree commited on
Commit
8f4f20a
·
verified ·
1 Parent(s): d8a0e5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -5,7 +5,7 @@ import requests
5
  import io
6
  from PIL import Image
7
  import os
8
- import time # Importing time to add delays for sequential execution
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 GPT-3 text generation function with error handling
32
- def generate_with_gpt3(prompt, max_tokens=150, temperature=0.7):
33
  try:
34
- print("Generating text with GPT-3...")
35
- response = openai.Completion.create(
36
- engine="text-davinci-003", # Use "text-davinci-003" for high-quality outputs
37
- prompt=prompt,
38
- max_tokens=max_tokens,
39
- temperature=temperature,
 
 
40
  top_p=0.9,
41
- frequency_penalty=0.0,
42
- presence_penalty=0.0
43
  )
44
- generated_text = response.choices[0].text.strip()
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
- # Ensure sequential flow by waiting before moving to the next step
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 GPT-3
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, max_tokens=150, temperature=0.7)
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
- # Ensure sequential flow by waiting before moving to the next step
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`