youzhang commited on
Commit
59eeb4a
·
verified ·
1 Parent(s): 0e71058

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -87
app.py CHANGED
@@ -1,79 +1,4 @@
1
- '''
2
- import gradio as gr
3
- from openai import OpenAI
4
- import os
5
- import time
6
-
7
- def predict(message, history, system_prompt, model, max_tokens, temperature, top_p):
8
-
9
- # Initialize the OpenAI client
10
- client = OpenAI(
11
- api_key=os.environ.get("API_TOKEN"),
12
- )
13
-
14
- # Start with the system prompt
15
- messages = [{"role": "system", "content": system_prompt}]
16
-
17
- # Add the conversation history
18
- messages.extend(history if history else [])
19
-
20
- # Add the current user message
21
- messages.append({"role": "user", "content": message})
22
 
23
- # Record the start time
24
- start_time = time.time()
25
-
26
- # Streaming response
27
- response = client.chat.completions.create(
28
- model=model,
29
- messages=messages,
30
- max_tokens=max_tokens,
31
- temperature=temperature,
32
- top_p=top_p,
33
- stop=None,
34
- stream=True
35
- )
36
-
37
- full_message = ""
38
- first_chunk_time = None
39
- last_yield_time = None
40
-
41
- for chunk in response:
42
- if chunk.choices and chunk.choices[0].delta.content:
43
- if first_chunk_time is None:
44
- first_chunk_time = time.time() - start_time # Record time for the first chunk
45
-
46
- full_message += chunk.choices[0].delta.content
47
- current_time = time.time()
48
- chunk_time = current_time - start_time # calculate the time delay of the chunk
49
- print(f"Message received {chunk_time:.2f} seconds after request: {chunk.choices[0].delta.content}")
50
-
51
- if last_yield_time is None or (current_time - last_yield_time >= 0.25):
52
- yield full_message
53
- last_yield_time = current_time
54
-
55
- # Ensure to yield any remaining message that didn't meet the time threshold
56
- if full_message:
57
- total_time = time.time() - start_time
58
- # Append timing information to the response message
59
- full_message += f" (First Chunk: {first_chunk_time:.2f}s, Total: {total_time:.2f}s)"
60
- yield full_message
61
-
62
- gr.ChatInterface(
63
- fn=predict,
64
- type="messages",
65
- #save_history=True,
66
- #editable=True,
67
- additional_inputs=[
68
- gr.Textbox("You are a helpful AI assistant.", label="System Prompt"),
69
- gr.Dropdown(["gpt-4o", "gpt-4o-mini"], label="Model"),
70
- gr.Slider(800, 4000, value=2000, label="Max Token"),
71
- gr.Slider(0, 1, value=0.7, label="Temperature"),
72
- gr.Slider(0, 1, value=0.95, label="Top P"),
73
- ],
74
- css="footer{display:none !important}"
75
- ).launch()
76
- '''
77
 
78
  import gradio as gr
79
  from openai import OpenAI
@@ -134,17 +59,21 @@ def predict(user_input, history, subject, model, max_tokens, temperature, top_p)
134
  full_message += f" (First Chunk: {first_chunk_time:.2f}s, Total: {total_time:.2f}s)"
135
  yield full_message
136
 
137
- # Function to generate image based on prompt
138
  def generate_image(prompt, size="256x256"):
139
- response = openai.Image.create(
140
- prompt=prompt,
141
- n=1,
142
- size=size
143
- )
144
- image_url = response['data'][0]['url']
145
- image_response = requests.get(image_url)
146
- image = Image.open(BytesIO(image_response.content))
147
- return image
 
 
 
 
 
148
 
149
  # Gradio interface
150
  with gr.Blocks(css="footer{display:none !important}") as demo:
@@ -170,9 +99,9 @@ with gr.Blocks(css="footer{display:none !important}") as demo:
170
  history.append([message, full_response])
171
 
172
  # Generate image based on the latest assistant response
173
- #image = generate_image(text_response)
174
 
175
- return history, ""
176
 
177
  user_input.submit(
178
  wrapped_predict,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import gradio as gr
4
  from openai import OpenAI
 
59
  full_message += f" (First Chunk: {first_chunk_time:.2f}s, Total: {total_time:.2f}s)"
60
  yield full_message
61
 
 
62
  def generate_image(prompt, size="256x256"):
63
+ try:
64
+ response = client.images.generate(
65
+ model="dall-e-3", # or "dall-e-2"
66
+ prompt=prompt,
67
+ size=size,
68
+ quality="standard",
69
+ n=1
70
+ )
71
+ image_url = response.data[0].url
72
+ image_response = requests.get(image_url)
73
+ return Image.open(BytesIO(image_response.content))
74
+ except Exception as e:
75
+ print("Image generation error:", e)
76
+ return None
77
 
78
  # Gradio interface
79
  with gr.Blocks(css="footer{display:none !important}") as demo:
 
99
  history.append([message, full_response])
100
 
101
  # Generate image based on the latest assistant response
102
+ image = generate_image(text_response)
103
 
104
+ return history, "", image
105
 
106
  user_input.submit(
107
  wrapped_predict,