Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import
|
| 4 |
-
import
|
| 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 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 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 |
-
|
| 38 |
model="Qwen/Qwen2-VL-7B-Instruct",
|
| 39 |
messages=messages,
|
| 40 |
-
max_tokens=1024
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 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(
|
|
|
|
| 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()
|