Spaces:
Runtime error
Runtime error
File size: 1,096 Bytes
1bfff27 |
1 2 3 4 5 6 7 8 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 |
import gradio as gr
import requests
import matplotlib.pyplot as plt
import io
API_KEY = "AIzaSyB_mGS8XANkAnb486014uIDf0tMG64eLX0"
def generate_plot(text_prompt):
# Gemini prompt
gemini_prompt = f"Generate Python matplotlib code to visualize: {text_prompt}"
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={API_KEY}"
headers = {"Content-Type": "application/json"}
data = {
"contents": [{"parts": [{"text": gemini_prompt}]}]
}
# Gemini response
response = requests.post(url, headers=headers, json=data)
code = response.json()['candidates'][0]['content']['parts'][0]['text']
# Plot from Gemini code
plt.clf()
exec(code, globals())
# Save plot as image
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
return buf.getvalue()
gr.Interface(fn=generate_plot,
inputs=gr.Textbox(label="Describe the Data to Visualize"),
outputs=gr.Image(type="file", label="Generated Graph"),
title="Text to Graph using Gemini AI").launch() |