Spaces:
Runtime error
Runtime error
requirements.txt
Browse filesgradio
requests
matplotlib
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import io
|
5 |
+
|
6 |
+
API_KEY = "AIzaSyB_mGS8XANkAnb486014uIDf0tMG64eLX0"
|
7 |
+
|
8 |
+
def generate_plot(text_prompt):
|
9 |
+
# Gemini prompt
|
10 |
+
gemini_prompt = f"Generate Python matplotlib code to visualize: {text_prompt}"
|
11 |
+
|
12 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={API_KEY}"
|
13 |
+
headers = {"Content-Type": "application/json"}
|
14 |
+
data = {
|
15 |
+
"contents": [{"parts": [{"text": gemini_prompt}]}]
|
16 |
+
}
|
17 |
+
|
18 |
+
# Gemini response
|
19 |
+
response = requests.post(url, headers=headers, json=data)
|
20 |
+
code = response.json()['candidates'][0]['content']['parts'][0]['text']
|
21 |
+
|
22 |
+
# Plot from Gemini code
|
23 |
+
plt.clf()
|
24 |
+
exec(code, globals())
|
25 |
+
|
26 |
+
# Save plot as image
|
27 |
+
buf = io.BytesIO()
|
28 |
+
plt.savefig(buf, format='png')
|
29 |
+
buf.seek(0)
|
30 |
+
|
31 |
+
return buf.getvalue()
|
32 |
+
|
33 |
+
gr.Interface(fn=generate_plot,
|
34 |
+
inputs=gr.Textbox(label="Describe the Data to Visualize"),
|
35 |
+
outputs=gr.Image(type="file", label="Generated Graph"),
|
36 |
+
title="Text to Graph using Gemini AI").launch()
|