Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,10 @@ import gradio as gr
|
|
| 8 |
|
| 9 |
# Set your Hugging Face API token
|
| 10 |
HUGGING_FACE_API_KEY = os.getenv("HUGGING_FACE_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
math_messages = []
|
| 12 |
|
| 13 |
# Function to process the image with Hugging Face API
|
|
@@ -36,8 +40,12 @@ def process_image(image, shouldConvert=False):
|
|
| 36 |
files={"file": img_file}
|
| 37 |
)
|
| 38 |
os.remove(filename) # Clean up temp file
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return caption
|
| 42 |
|
| 43 |
# Function for getting math responses from Hugging Face's text generation API
|
|
@@ -61,8 +69,13 @@ def get_math_response(image_description, user_question):
|
|
| 61 |
headers={"Authorization": f"Bearer {HUGGING_FACE_API_KEY}"},
|
| 62 |
json=payload
|
| 63 |
)
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
yield answer
|
| 67 |
math_messages.append({"role": "assistant", "content": answer})
|
| 68 |
|
|
|
|
| 8 |
|
| 9 |
# Set your Hugging Face API token
|
| 10 |
HUGGING_FACE_API_KEY = os.getenv("HUGGING_FACE_API_KEY")
|
| 11 |
+
|
| 12 |
+
if not HUGGING_FACE_API_KEY:
|
| 13 |
+
raise ValueError("Please set the Hugging Face API key in the environment as 'HUGGING_FACE_API_KEY'.")
|
| 14 |
+
|
| 15 |
math_messages = []
|
| 16 |
|
| 17 |
# Function to process the image with Hugging Face API
|
|
|
|
| 40 |
files={"file": img_file}
|
| 41 |
)
|
| 42 |
os.remove(filename) # Clean up temp file
|
| 43 |
+
|
| 44 |
+
# Check if response is successful and handle errors
|
| 45 |
+
if response.status_code == 200:
|
| 46 |
+
caption = response.json().get("generated_text", "No description available.")
|
| 47 |
+
else:
|
| 48 |
+
caption = f"Error: {response.status_code} - {response.json().get('error', 'Unknown error')}"
|
| 49 |
return caption
|
| 50 |
|
| 51 |
# Function for getting math responses from Hugging Face's text generation API
|
|
|
|
| 69 |
headers={"Authorization": f"Bearer {HUGGING_FACE_API_KEY}"},
|
| 70 |
json=payload
|
| 71 |
)
|
| 72 |
+
|
| 73 |
+
# Check if response is successful and handle errors
|
| 74 |
+
if response.status_code == 200:
|
| 75 |
+
answer = response.json().get("generated_text", "Sorry, I couldn't generate a response.")
|
| 76 |
+
else:
|
| 77 |
+
answer = f"Error: {response.status_code} - {response.json().get('error', 'Unknown error')}"
|
| 78 |
+
|
| 79 |
yield answer
|
| 80 |
math_messages.append({"role": "assistant", "content": answer})
|
| 81 |
|