Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,176 +1,176 @@
|
|
1 |
-
# Requirement: !pip install gradio, groq
|
2 |
-
|
3 |
-
# 1. Imports and API setup
|
4 |
-
import gradio as gr
|
5 |
-
from groq import Groq
|
6 |
-
import base64
|
7 |
-
import os
|
8 |
-
import spaces
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
return
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
{
|
55 |
-
"role": "
|
56 |
-
"content":
|
57 |
-
}
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
#
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
#
|
97 |
-
text-align: center;
|
98 |
-
margin
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
<
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
with gr.Row():
|
141 |
-
|
142 |
-
with gr.Row():
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
# Start the Gradio interface
|
176 |
gradio_interface()
|
|
|
1 |
+
# Requirement: !pip install gradio, groq
|
2 |
+
|
3 |
+
# 1. Imports and API setup
|
4 |
+
import gradio as gr
|
5 |
+
from groq import Groq
|
6 |
+
import base64
|
7 |
+
import os
|
8 |
+
import spaces
|
9 |
+
|
10 |
+
# Define models used in the process
|
11 |
+
llava_model = 'llava-v1.5-7b-4096-preview'
|
12 |
+
llama31_model = 'llama-3.1-70b-versatile'
|
13 |
+
|
14 |
+
# Image encoding function
|
15 |
+
def encode_image(image_path):
|
16 |
+
with open(image_path, "rb") as image_file:
|
17 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
18 |
+
|
19 |
+
# Image to text function
|
20 |
+
def image_to_text(client, model, base64_image, prompt):
|
21 |
+
try:
|
22 |
+
chat_completion = client.chat.completions.create(
|
23 |
+
messages=[
|
24 |
+
{
|
25 |
+
"role": "user",
|
26 |
+
"content": [
|
27 |
+
{"type": "text", "text": prompt},
|
28 |
+
{
|
29 |
+
"type": "image_url",
|
30 |
+
"image_url": {
|
31 |
+
"url": f"data:image/jpeg;base64,{base64_image}",
|
32 |
+
},
|
33 |
+
},
|
34 |
+
],
|
35 |
+
}
|
36 |
+
],
|
37 |
+
model=model
|
38 |
+
)
|
39 |
+
return chat_completion.choices[0].message.content
|
40 |
+
except Exception as e:
|
41 |
+
if 'Invalid API Key' in str(e):
|
42 |
+
return "Please enter a correct API key and try again."
|
43 |
+
return f"Error generating text from image: {str(e)}"
|
44 |
+
|
45 |
+
# Technical review generation function
|
46 |
+
def technical_review_generation(client, image_description):
|
47 |
+
keywords = ["econometrics", "finance", "marketing", "stock", "prediction", "chart", "graph", "time series"]
|
48 |
+
if not any(keyword in image_description.lower() for keyword in keywords):
|
49 |
+
return "The image is not related to the area this app covers. Please input a relevant image."
|
50 |
+
|
51 |
+
try:
|
52 |
+
chat_completion = client.chat.completions.create(
|
53 |
+
messages=[
|
54 |
+
{
|
55 |
+
"role": "system",
|
56 |
+
"content": "You are a professional econometrics. Write a complete review and report about the scene depicted in this image.",
|
57 |
+
},
|
58 |
+
{
|
59 |
+
"role": "user",
|
60 |
+
"content": image_description,
|
61 |
+
}
|
62 |
+
],
|
63 |
+
model=llama31_model
|
64 |
+
)
|
65 |
+
return chat_completion.choices[0].message.content
|
66 |
+
except Exception as e:
|
67 |
+
return f"Error generating report: {str(e)}"
|
68 |
+
|
69 |
+
# Main function for Gradio interface
|
70 |
+
def process_image(api_key, image, prompt="Describe this image in detail."):
|
71 |
+
# Set the API key
|
72 |
+
try:
|
73 |
+
os.environ["GROQ_API_KEY"] = api_key
|
74 |
+
client = Groq() # Initialize the Groq client with the provided key
|
75 |
+
except Exception as e:
|
76 |
+
return "Please enter a correct API key and try again.", ""
|
77 |
+
|
78 |
+
# Encode the image
|
79 |
+
base64_image = encode_image(image)
|
80 |
+
|
81 |
+
# Get image description from the model
|
82 |
+
image_description = image_to_text(client, llava_model, base64_image, prompt)
|
83 |
+
|
84 |
+
# If API key was invalid, only return the API key error message
|
85 |
+
if "Please enter a correct API key and try again." in image_description:
|
86 |
+
return image_description, ""
|
87 |
+
|
88 |
+
# Generate the econometrics report based on the image description
|
89 |
+
report = technical_review_generation(client, image_description)
|
90 |
+
|
91 |
+
# Return both image description and the econometrics report
|
92 |
+
return f"--- Image Description ---\n{image_description}", f"--- GroqLLaVA EconoMind Report ---\n{report}"
|
93 |
+
|
94 |
+
# Define CSS for centering elements and footer styling
|
95 |
+
css = """
|
96 |
+
#title, #description {
|
97 |
+
text-align: center;
|
98 |
+
margin: 20px;
|
99 |
+
}
|
100 |
+
#footer {
|
101 |
+
text-align: center;
|
102 |
+
margin-top: 30px;
|
103 |
+
padding: 10px;
|
104 |
+
font-size: 14px;
|
105 |
+
}
|
106 |
+
.gradio-container {
|
107 |
+
display: flex;
|
108 |
+
flex-direction: column;
|
109 |
+
align-items: center;
|
110 |
+
}
|
111 |
+
.gradio-row {
|
112 |
+
width: 100%;
|
113 |
+
display: flex;
|
114 |
+
justify-content: center;
|
115 |
+
}
|
116 |
+
.clear-button {
|
117 |
+
margin-top: 10px;
|
118 |
+
}
|
119 |
+
"""
|
120 |
+
|
121 |
+
# Gradio Interface
|
122 |
+
|
123 |
+
def gradio_interface():
|
124 |
+
# Define the footer HTML
|
125 |
+
footer = """
|
126 |
+
<div id="footer">
|
127 |
+
<a href="https://www.linkedin.com/in/pejman-ebrahimi-4a60151a7/" target="_blank">LinkedIn</a> |
|
128 |
+
<a href="https://github.com/arad1367" target="_blank">GitHub</a> |
|
129 |
+
<a href="https://arad1367.pythonanywhere.com/" target="_blank">Live demo of my PhD defense</a> |
|
130 |
+
<a href="https://groq.com/introducing-llava-v1-5-7b-on-groqcloud-unlocking-the-power-of-multimodal-ai/" target="_blank">Introducing LLaVA V1.5 7B on GroqCloud</a>
|
131 |
+
<br>
|
132 |
+
Made with π by Pejman Ebrahimi
|
133 |
+
</div>
|
134 |
+
"""
|
135 |
+
@spaces.GPU()
|
136 |
+
with gr.Blocks(theme="gradio/soft", css=css) as demo:
|
137 |
+
gr.HTML("<h1 id='title'>GroqLLaVA Econometrics Agent</h1>")
|
138 |
+
gr.HTML("<p id='description'>Upload an economic chart and get a detailed analysis using Groq + LLaVA V1.5 7B multimodal.</p>")
|
139 |
+
|
140 |
+
with gr.Row():
|
141 |
+
api_key_input = gr.Textbox(label="GROQ API Key", placeholder="Enter your GROQ API Key", type="password")
|
142 |
+
with gr.Row():
|
143 |
+
image_input = gr.Image(type="filepath", label="Upload an Image") # Changed type to 'filepath'
|
144 |
+
with gr.Row():
|
145 |
+
report_button = gr.Button("Generate Report")
|
146 |
+
with gr.Row():
|
147 |
+
output_description = gr.Textbox(label="Image Description", lines=10, elem_id="description-box")
|
148 |
+
output_report = gr.Textbox(label="Report", lines=10, elem_id="report-box")
|
149 |
+
|
150 |
+
# Define the interaction between inputs and outputs
|
151 |
+
report_button.click(
|
152 |
+
fn=process_image,
|
153 |
+
inputs=[api_key_input, image_input],
|
154 |
+
outputs=[output_description, output_report]
|
155 |
+
)
|
156 |
+
|
157 |
+
# Add footer HTML
|
158 |
+
gr.HTML(footer)
|
159 |
+
|
160 |
+
# Add clear button
|
161 |
+
def clear_inputs():
|
162 |
+
return "", None, "", ""
|
163 |
+
|
164 |
+
with gr.Row():
|
165 |
+
clear_button = gr.Button("Clear", elem_id="clear-button")
|
166 |
+
clear_button.click(
|
167 |
+
fn=clear_inputs,
|
168 |
+
inputs=[],
|
169 |
+
outputs=[api_key_input, image_input, output_description, output_report]
|
170 |
+
)
|
171 |
+
|
172 |
+
# Launch the interface
|
173 |
+
demo.launch()
|
174 |
+
|
175 |
+
# Start the Gradio interface
|
176 |
gradio_interface()
|