candenizkocak commited on
Commit
e629bd5
1 Parent(s): 34b833b

Initial Commit

Browse files
Files changed (2) hide show
  1. app.py +128 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ import io
4
+ import base64
5
+ from PIL import Image
6
+ from groq import Groq
7
+
8
+ # Define the function to encode the image
9
+ def encode_image(image_path):
10
+ with open(image_path, "rb") as image_file:
11
+ return base64.b64encode(image_file.read()).decode("utf-8")
12
+
13
+ # Function to run the user input code and display the plot
14
+ def run_code(code, groq_api_key):
15
+ try:
16
+ # Setup Groq API client with the provided API key
17
+ llava_client = Groq(api_key=groq_api_key)
18
+ llama_client = Groq(api_key=groq_api_key)
19
+
20
+ fig, ax = plt.subplots()
21
+
22
+ # Create a safe environment to execute code
23
+ exec(code, {"plt": plt, "ax": ax})
24
+
25
+ # Save the plot to a byte buffer
26
+ buf = io.BytesIO()
27
+ plt.savefig(buf, format="png", bbox_inches="tight")
28
+ buf.seek(0)
29
+
30
+ # Open the saved image and resize it if necessary
31
+ img = Image.open(buf)
32
+ max_width, max_height = 600, 400 # Set maximum display size
33
+ img.thumbnail((max_width, max_height)) # Resize to fit
34
+
35
+ # Save the image to the disk
36
+ img.save("plot.png")
37
+ buf.seek(0)
38
+
39
+ # Encode the image for Groq API
40
+ base64_image = encode_image("plot.png")
41
+
42
+ # Sending the plot image to LLava API to get the description
43
+ llava_completion = llava_client.chat.completions.create(
44
+ model="llava-v1.5-7b-4096-preview",
45
+ messages=[{
46
+ "role": "user",
47
+ "content": [
48
+ {"type": "text", "text": f"Describe the plot values with image and the code provided to you. Code: {code}\n"},
49
+ {
50
+ "type": "image_url",
51
+ "image_url": {"url": f"data:image/png;base64,{base64_image}"}
52
+ },
53
+ ],
54
+ }],
55
+ temperature=0.5,
56
+ max_tokens=4096,
57
+ top_p=1,
58
+ stream=False,
59
+ stop=None,
60
+ )
61
+
62
+ # Extract the LLava description from the API response
63
+ llava_description = llava_completion.choices[0].message.content
64
+
65
+ # Sending the plot image to Llama 3.1 API to get the description
66
+ llama_completion = llama_client.chat.completions.create(
67
+ model="llama-3.1-70b-versatile",
68
+ messages=[
69
+ {
70
+ "role": "system",
71
+ "content": "What are the details of the plot provided by the user. Point out the important things in the dataset. What is the purpose of this dataset and how to interpret it. Analyze it."
72
+ },
73
+ {
74
+ "role": "user",
75
+ "content": code
76
+ }
77
+ ],
78
+ temperature=0,
79
+ max_tokens=4096,
80
+ top_p=1,
81
+ stream=True,
82
+ stop=None,
83
+ )
84
+
85
+ # Extract the Llama 3.1 description from the API response
86
+ llama_description = ""
87
+ for chunk in llama_completion:
88
+ llama_description += chunk.choices[0].delta.content or ""
89
+
90
+ return img, llava_description, llama_description
91
+
92
+ except Exception as e:
93
+ return None, f"Error: {str(e)}", None
94
+ finally:
95
+ plt.close(fig)
96
+
97
+ # Define the Gradio interface
98
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
99
+ gr.Markdown("""## Plot and Describe
100
+
101
+ **⚠️ Disclaimer:** Generative models may hallucinate or produce incorrect outputs. This tool is built for demonstration purposes only and should not be relied upon for critical analysis or decision-making.
102
+ """, elem_id="disclaimer")
103
+
104
+ with gr.Row():
105
+ api_key_input = gr.Textbox(
106
+ label="Groq API Key", type="password", placeholder="Enter your Groq API key here"
107
+ )
108
+
109
+ with gr.Row():
110
+ code_input = gr.Code(
111
+ language="python", lines=20, label="Input Code"
112
+ )
113
+ output_image = gr.Image(type="pil", label="Chart will be displayed here")
114
+
115
+ submit_btn = gr.Button("Submit")
116
+
117
+ with gr.Row():
118
+ output_llava_text = gr.Textbox(label="Description from LLaVa", interactive=False)
119
+ output_llama_text = gr.Textbox(label="Description from Llama 3.1", interactive=False)
120
+
121
+ submit_btn.click(
122
+ fn=run_code,
123
+ inputs=[code_input, api_key_input],
124
+ outputs=[output_image, output_llava_text, output_llama_text]
125
+ )
126
+
127
+ # Launch the interface
128
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==3.42.0
2
+ matplotlib==3.7.2
3
+ pillow==9.5.0
4
+ groq