Spaces:
Sleeping
Sleeping
fixed the problem with the API key
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import os
|
2 |
-
import time
|
3 |
import base64
|
4 |
import requests
|
5 |
import streamlit as st
|
@@ -7,12 +6,7 @@ import streamlit as st
|
|
7 |
# OpenAI API Key
|
8 |
# Access the secret API key
|
9 |
# if the app is running locally, you can set the API key as an environment variable
|
10 |
-
api_key = os.getenv("
|
11 |
-
|
12 |
-
# Function to encode the image
|
13 |
-
def encode_image(image_path):
|
14 |
-
with open(image_path, "rb") as image_file:
|
15 |
-
return base64.b64encode(image_file.read()).decode('utf-8')
|
16 |
|
17 |
headers = {
|
18 |
"Content-Type": "application/json",
|
@@ -20,7 +14,7 @@ headers = {
|
|
20 |
}
|
21 |
|
22 |
def main():
|
23 |
-
st.title("Multimodal using GPT
|
24 |
|
25 |
text = """Prof. Louie F. Cervantes, M. Eng. (Information Engineering)
|
26 |
CCS 229 - Intelligent Systems
|
@@ -40,7 +34,7 @@ def main():
|
|
40 |
base64_image = base64.b64encode(uploaded_image.getvalue()).decode('utf-8')
|
41 |
|
42 |
# Display the uploaded image
|
43 |
-
st.image(uploaded_image, caption="Uploaded Image",
|
44 |
|
45 |
# List of image analysis tasks
|
46 |
analysis_tasks = [
|
@@ -53,7 +47,8 @@ def main():
|
|
53 |
"Optical Character Recognition (OCR): Extract text from the image, such as printed or handwritten text.",
|
54 |
"Diagram Understanding: Analyze a diagram (e.g., flowchart, circuit diagram) and extract its structure and meaning.",
|
55 |
"Art Analysis: Describe the artistic style, subject matter, and emotional impact of an image.",
|
56 |
-
"Medical Image Analysis: Analyze medical images (e.g., X-rays, MRIs) to detect abnormalities or diagnose diseases."
|
|
|
57 |
]
|
58 |
|
59 |
# Task selection dropdown
|
@@ -61,8 +56,8 @@ def main():
|
|
61 |
|
62 |
# Button to generate response
|
63 |
if st.button("Generate Response"):
|
64 |
-
if uploaded_image is None or selected_task
|
65 |
-
st.error("Please upload an image and
|
66 |
else:
|
67 |
# Prepare the multimodal prompt
|
68 |
payload = {
|
@@ -90,13 +85,20 @@ def main():
|
|
90 |
with st.spinner("Processing..."):
|
91 |
# Generate response
|
92 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
main()
|
|
|
1 |
import os
|
|
|
2 |
import base64
|
3 |
import requests
|
4 |
import streamlit as st
|
|
|
6 |
# OpenAI API Key
|
7 |
# Access the secret API key
|
8 |
# if the app is running locally, you can set the API key as an environment variable
|
9 |
+
api_key = os.getenv("OPENAI_API_KEY") # Make sure this environment variable is set correctly
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
headers = {
|
12 |
"Content-Type": "application/json",
|
|
|
14 |
}
|
15 |
|
16 |
def main():
|
17 |
+
st.title("Multimodal using GPT-4 Turbo Model")
|
18 |
|
19 |
text = """Prof. Louie F. Cervantes, M. Eng. (Information Engineering)
|
20 |
CCS 229 - Intelligent Systems
|
|
|
34 |
base64_image = base64.b64encode(uploaded_image.getvalue()).decode('utf-8')
|
35 |
|
36 |
# Display the uploaded image
|
37 |
+
st.image(uploaded_image, caption="Uploaded Image", use_container_width=True)
|
38 |
|
39 |
# List of image analysis tasks
|
40 |
analysis_tasks = [
|
|
|
47 |
"Optical Character Recognition (OCR): Extract text from the image, such as printed or handwritten text.",
|
48 |
"Diagram Understanding: Analyze a diagram (e.g., flowchart, circuit diagram) and extract its structure and meaning.",
|
49 |
"Art Analysis: Describe the artistic style, subject matter, and emotional impact of an image.",
|
50 |
+
"Medical Image Analysis: Analyze medical images (e.g., X-rays, MRIs) to detect abnormalities or diagnose diseases.",
|
51 |
+
"Obtaining text data: Extract text from the image, such as names, date, time, etc.",
|
52 |
]
|
53 |
|
54 |
# Task selection dropdown
|
|
|
56 |
|
57 |
# Button to generate response
|
58 |
if st.button("Generate Response"):
|
59 |
+
if uploaded_image is None or not selected_task: # Check if a task is selected
|
60 |
+
st.error("Please upload an image and select a task.")
|
61 |
else:
|
62 |
# Prepare the multimodal prompt
|
63 |
payload = {
|
|
|
85 |
with st.spinner("Processing..."):
|
86 |
# Generate response
|
87 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
88 |
+
|
89 |
+
# Check for errors in the response
|
90 |
+
if response.status_code != 200:
|
91 |
+
st.error(f"Error: {response.status_code} - {response.text}")
|
92 |
+
else:
|
93 |
+
# Display the response
|
94 |
+
content = response.json()
|
95 |
+
|
96 |
+
# Show the response content
|
97 |
+
st.success("Response generated!")
|
98 |
+
# Extract the content of the response
|
99 |
+
# Access the content correctly from the response JSON
|
100 |
+
contentstring = content['choices'][0]['message']['content']
|
101 |
+
st.markdown(f"AI Response: {contentstring}")
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
main()
|