arad1367 commited on
Commit
214dc61
Β·
verified Β·
1 Parent(s): e05c66e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -175
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
- # 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()
 
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()