oscurantismo commited on
Commit
60e6d37
·
verified ·
1 Parent(s): d49afa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import gradio as gr
3
  import openai
 
4
  from PIL import Image, ImageEnhance
5
  import cv2
6
  import torch
@@ -31,27 +32,32 @@ object_labels = [
31
  EXAMPLE_IMAGE_URL = "https://www.watercoloraffair.com/wp-content/uploads/2023/04/monet-houses-of-parliament-low-key.jpg" # Square example image
32
  example_image = Image.open(BytesIO(requests.get(EXAMPLE_IMAGE_URL).content))
33
 
 
 
 
34
  def process_chat(user_text):
35
  if not user_text.strip():
36
  yield "⚠️ Please enter a valid question."
37
  return
38
 
39
  try:
40
- # Use ChatCompletion API for chat models
41
- response = openai.ChatCompletion.create(
42
- model="gpt-4",
43
  messages=[
44
  {"role": "system", "content": "You are a helpful assistant named Diane specializing in digital art advice."},
45
  {"role": "user", "content": user_text},
46
  ],
47
  stream=True # Enable streaming
48
  )
 
49
  response_text = ""
50
  for chunk in response:
51
- if "choices" in chunk and chunk.choices[0].delta.get("content"):
52
  token = chunk.choices[0].delta["content"]
53
  response_text += token
54
  yield response_text
 
55
  except Exception as e:
56
  yield f"❌ An error occurred: {str(e)}"
57
 
 
1
  import os
2
  import gradio as gr
3
  import openai
4
+ from openai import OpenAI
5
  from PIL import Image, ImageEnhance
6
  import cv2
7
  import torch
 
32
  EXAMPLE_IMAGE_URL = "https://www.watercoloraffair.com/wp-content/uploads/2023/04/monet-houses-of-parliament-low-key.jpg" # Square example image
33
  example_image = Image.open(BytesIO(requests.get(EXAMPLE_IMAGE_URL).content))
34
 
35
+ # Initialize OpenAI client
36
+ client = OpenAI()
37
+
38
  def process_chat(user_text):
39
  if not user_text.strip():
40
  yield "⚠️ Please enter a valid question."
41
  return
42
 
43
  try:
44
+ # Use the client to create a completion
45
+ response = client.chat.completions.create(
46
+ model="gpt-4o",
47
  messages=[
48
  {"role": "system", "content": "You are a helpful assistant named Diane specializing in digital art advice."},
49
  {"role": "user", "content": user_text},
50
  ],
51
  stream=True # Enable streaming
52
  )
53
+
54
  response_text = ""
55
  for chunk in response:
56
+ if chunk.choices[0].delta.get("content"):
57
  token = chunk.choices[0].delta["content"]
58
  response_text += token
59
  yield response_text
60
+
61
  except Exception as e:
62
  yield f"❌ An error occurred: {str(e)}"
63