yasserrmd commited on
Commit
dc194a9
·
verified ·
1 Parent(s): de84813

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -1,18 +1,18 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- import os
4
- import tempfile
5
 
6
  # Initialize the Hugging Face Inference Client
7
  client = InferenceClient()
8
 
9
  # Function to analyze plant images
10
  def analyze_plant_image(image):
11
- # Save image to a temporary location with appropriate extension
12
- temp_dir = tempfile.mkdtemp()
13
- temp_image_path = os.path.join(temp_dir, "uploaded_image.jpg")
14
- image.save(temp_image_path, format="JPEG")
15
- image_url = f"file://{temp_image_path}"
16
 
17
  # Create the message structure
18
  messages = [
@@ -34,15 +34,18 @@ def analyze_plant_image(image):
34
  ]
35
 
36
  # Create the completion request
37
- completion = client.chat_completions.create(
38
  model="Qwen/Qwen2-VL-7B-Instruct",
39
  messages=messages,
40
- max_tokens=1024
 
41
  )
42
 
43
- # Extract the response
44
- description = completion.choices[0].message["content"]
45
- return description
 
 
46
 
47
  # Create Gradio interface
48
  with gr.Blocks() as app:
@@ -63,4 +66,4 @@ with gr.Blocks() as app:
63
  analyze_button.click(fn=analyze_plant_image, inputs=image_input, outputs=output_markdown)
64
 
65
  # Run the Gradio app
66
- app.launch(debug=True)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import base64
4
+ import io
5
 
6
  # Initialize the Hugging Face Inference Client
7
  client = InferenceClient()
8
 
9
  # Function to analyze plant images
10
  def analyze_plant_image(image):
11
+ buffered = io.BytesIO()
12
+ image.save(buffered, format="JPEG")
13
+ image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
14
+ image_url = f"data:image/jpeg;base64,{image_base64}"
15
+
16
 
17
  # Create the message structure
18
  messages = [
 
34
  ]
35
 
36
  # Create the completion request
37
+ stream = client.chat.completions.create(
38
  model="Qwen/Qwen2-VL-7B-Instruct",
39
  messages=messages,
40
+ max_tokens=1024,
41
+ stream=True
42
  )
43
 
44
+ # Stream content as it is generated
45
+ output_text = ""
46
+ for chunk in stream:
47
+ output_text += chunk.choices[0].delta.content
48
+ yield output_text
49
 
50
  # Create Gradio interface
51
  with gr.Blocks() as app:
 
66
  analyze_button.click(fn=analyze_plant_image, inputs=image_input, outputs=output_markdown)
67
 
68
  # Run the Gradio app
69
+ app.launch()