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