Spaces:
Sleeping
Sleeping
File size: 2,557 Bytes
3a4f5e9 | 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import streamlit as st
import google.generativeai as genai
import os
API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=API_KEY)
def generate_app_code(framework, task):
"""
Generates Python code for the selected framework and task using the AI model.
Args:
framework (str): The selected framework ('Streamlit' or 'Gradio').
task (str): The task for which the app will be generated.
Returns:
str: Generated Python code or an error message.
"""
try:
# Construct the prompt
prompt = (
f"Create a {framework} app for the following task: {task}. "
"Provide the full Python code and ensure it is functional."
)
# Send the prompt to the model
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content(prompt)
return response.text
except Exception as e:
return f"An error occurred: {e}"
def main():
# Streamlit UI
st.title("App Builder: Streamlit or Gradio")
with st.expander("ℹ️ About"):
st.write(
"This tool generates Python code for a Streamlit or Gradio app based on a selected task. "
"It uses the Gemini 2.5 Pro model to generate the code. "
"You can select a predefined task or enter a custom one.")
# Step 1: Select the framework
framework = st.selectbox("Select a framework:", ["Streamlit", "Gradio"])
# Step 2: Select a task or enter a custom one
predefined_tasks = [
"Interactive Data Explorer",
"Simple Linear Regression",
"Image Classification with Pre-trained Model",
"Text Summarizer",
"Sentiment Analysis Tool",
"Interactive Quiz App",
"Basic Calculator",
"Unit Converter",
"Color Mixer",
"Simple Game (e.g., Number Guessing)"
]
task = st.selectbox("Select a predefined task:", predefined_tasks)
custom_task = st.text_input("Or enter a custom task:")
# Use the custom task if provided
task = custom_task if custom_task.strip() else task
# Step 3: Generate the app code
if st.button("Generate App Code"):
with st.spinner("Generating code..."):
app_code = generate_app_code(framework, task)
if app_code:
st.subheader("Generated Code")
st.code(app_code, language="python")
else:
st.error("Failed to generate the app code. Please try again.")
if __name__ == "__main__":
main() |