Spaces:
Runtime error
Runtime error
| 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() |