PerryCheng614 commited on
Commit
70a6a62
1 Parent(s): 6978e20

change to http requests

Browse files
.gradio/flagged/Upload Image/7f96a1c033f4c556e208/example_2.jpg ADDED
.gradio/flagged/dataset1.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Upload Image,Question,Max Tokens,Response,timestamp
2
+ .gradio/flagged/Upload Image/7f96a1c033f4c556e208/example_2.jpg,What color is this dress? ,128,"Based on the image provided, the cat appears to be a domestic short-haired breed, characterized by its short fur and the distinctive pattern of its coat. The coloration and markings are reminiscent of a tabby, which is a common coat pattern in many domestic cats. The tabby pattern often includes stripes, dots, and swirling patterns along the coat, which can be seen on this cat. However, without more information or a view of the cat's body shape and size, it is not possible to determine the exact breed with certainty. Cats with similar color patterns, such as the one pictured, can be found in many breeds, including but",2024-12-17 22:20:30.337640
app.py CHANGED
@@ -1,62 +1,82 @@
1
  import gradio as gr
2
- import websockets
3
- import asyncio
4
  import json
5
  import base64
6
  from PIL import Image
7
  import io
 
 
8
 
9
- async def process_image_stream(image_path, prompt, max_tokens=512):
 
 
 
 
 
 
10
  """
11
- Process image with streaming response via WebSocket
12
  """
13
  if not image_path:
14
  yield "Please upload an image first."
15
  return
16
 
17
  try:
18
- # Read and convert image to base64
19
- with Image.open(image_path) as img:
20
- img = img.convert('RGB')
21
- buffer = io.BytesIO()
22
- img.save(buffer, format="JPEG")
23
- base64_image = base64.b64encode(buffer.getvalue()).decode('utf-8')
 
 
 
 
 
 
 
24
 
25
- # Connect to WebSocket
26
- async with websockets.connect('wss://nexa-omni.nexa4ai.com/ws/process-image/') as websocket:
27
- # Send image data and parameters as JSON
28
- await websocket.send(json.dumps({
29
- "image": f"data:image/jpeg;base64,{base64_image}",
30
- "prompt": prompt,
31
- "task": "instruct", # Fixed to instruct
32
- "max_tokens": max_tokens
33
- }))
34
 
 
 
 
 
35
  # Initialize response and token counter
36
- response = ""
37
  token_count = 0
38
 
39
- # Receive streaming response
40
- async for message in websocket:
41
- try:
42
- data = json.loads(message)
43
- if data["status"] == "generating":
44
- # Skip first three tokens if they match specific patterns
45
- if token_count < 3 and data["token"] in [" ", " \n", "\n", "<|im_start|>", "assistant"]:
46
- token_count += 1
 
 
 
 
 
 
 
 
 
 
 
 
47
  continue
48
- response += data["token"]
49
- yield response
50
- elif data["status"] == "complete":
51
- break
52
- elif data["status"] == "error":
53
- yield f"Error: {data['error']}"
54
- break
55
- except json.JSONDecodeError:
56
- continue
57
 
58
  except Exception as e:
59
- yield f"Error connecting to server: {str(e)}"
60
 
61
  # Create Gradio interface
62
  demo = gr.Interface(
 
1
  import gradio as gr
2
+ import requests
 
3
  import json
4
  import base64
5
  from PIL import Image
6
  import io
7
+ import os
8
+ from dotenv import load_dotenv
9
 
10
+ # Load environment variables
11
+ load_dotenv()
12
+ API_KEY = os.getenv("API_KEY")
13
+ if not API_KEY:
14
+ raise ValueError("API_KEY environment variable must be set")
15
+
16
+ def process_image_stream(image_path, prompt, max_tokens=512):
17
  """
18
+ Process image with streaming response via HTTP
19
  """
20
  if not image_path:
21
  yield "Please upload an image first."
22
  return
23
 
24
  try:
25
+ # Read and prepare image file
26
+ with open(image_path, 'rb') as img_file:
27
+ files = {
28
+ 'image': ('image.jpg', img_file, 'image/jpeg')
29
+ }
30
+ data = {
31
+ 'prompt': prompt,
32
+ 'task': 'instruct',
33
+ 'max_tokens': max_tokens
34
+ }
35
+ headers = {
36
+ 'X-API-Key': API_KEY
37
+ }
38
 
39
+ # Make streaming request
40
+ response = requests.post(
41
+ 'https://nexa-omni.nexa4ai.com/process-image/',
42
+ files=files,
43
+ data=data,
44
+ headers=headers,
45
+ stream=True
46
+ )
 
47
 
48
+ if response.status_code != 200:
49
+ yield f"Error: Server returned status code {response.status_code}"
50
+ return
51
+
52
  # Initialize response and token counter
53
+ response_text = ""
54
  token_count = 0
55
 
56
+ # Process the streaming response
57
+ for line in response.iter_lines():
58
+ if line:
59
+ line = line.decode('utf-8')
60
+ if line.startswith('data: '):
61
+ try:
62
+ data = json.loads(line[6:]) # Skip 'data: ' prefix
63
+ if data["status"] == "generating":
64
+ # Skip first three tokens if they match specific patterns
65
+ if token_count < 3 and data["token"] in [" ", " \n", "\n", "<|im_start|>", "assistant"]:
66
+ token_count += 1
67
+ continue
68
+ response_text += data["token"]
69
+ yield response_text
70
+ elif data["status"] == "complete":
71
+ break
72
+ elif data["status"] == "error":
73
+ yield f"Error: {data['error']}"
74
+ break
75
+ except json.JSONDecodeError:
76
  continue
 
 
 
 
 
 
 
 
 
77
 
78
  except Exception as e:
79
+ yield f"Error processing request: {str(e)}"
80
 
81
  # Create Gradio interface
82
  demo = gr.Interface(