Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 7 |
+
from langchain_chroma import Chroma
|
| 8 |
+
from langchain.chains import RetrievalQA
|
| 9 |
+
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
from langchain.prompts import PromptTemplate
|
| 11 |
+
from langchain.schema import AIMessage, HumanMessage
|
| 12 |
+
|
| 13 |
+
# Set OpenAI API Key
|
| 14 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 15 |
+
|
| 16 |
+
# Initialize OpenAI Chat Model
|
| 17 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.3, openai_api_key=OPENAI_API_KEY)
|
| 18 |
+
|
| 19 |
+
# Initialize ChromaDB Vector Store
|
| 20 |
+
vectorstore = Chroma(embedding_function=OpenAIEmbeddings())
|
| 21 |
+
|
| 22 |
+
# Define Prompt Template
|
| 23 |
+
prompt_template = PromptTemplate(
|
| 24 |
+
input_variables=["language", "description"],
|
| 25 |
+
template="""
|
| 26 |
+
You are an **expert AI coding assistant** proficient in **ALL web development languages**, including:
|
| 27 |
+
- HTML, CSS, JavaScript, React, Vue.js, Angular, Svelte
|
| 28 |
+
- Node.js, Express.js, Django, Flask, Ruby on Rails
|
| 29 |
+
- PHP, Laravel, ASP.NET, .NET Core, C#
|
| 30 |
+
- Python, TypeScript, GraphQL, SQL, NoSQL
|
| 31 |
+
- WebSockets, REST APIs, WebAssembly, Web3.js
|
| 32 |
+
|
| 33 |
+
**Task:**
|
| 34 |
+
Generate a **complete, structured, and functional** code snippet based on the user request.
|
| 35 |
+
|
| 36 |
+
**Language:** {language}
|
| 37 |
+
**Description:** {description}
|
| 38 |
+
|
| 39 |
+
- Ensure the code follows best practices and industry standards.
|
| 40 |
+
- Include comments explaining key sections.
|
| 41 |
+
- If applicable, provide installation/setup instructions.
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Streamlit UI
|
| 46 |
+
st.set_page_config(page_title="Intelligent Coding Agent", layout="wide")
|
| 47 |
+
st.title("🧠💻 Intelligent Coding Agent")
|
| 48 |
+
st.markdown("### Generate and Run Code in Any Web Development Language!")
|
| 49 |
+
|
| 50 |
+
# Language selection
|
| 51 |
+
languages = ["Python", "JavaScript", "PHP", "C#", "HTML/CSS/JS"]
|
| 52 |
+
language = st.selectbox("Select a programming language:", languages)
|
| 53 |
+
|
| 54 |
+
# User input for code description
|
| 55 |
+
description = st.text_area("Describe the code you need:", placeholder="E.g., Build a login system in PHP")
|
| 56 |
+
|
| 57 |
+
# Generate button
|
| 58 |
+
if st.button("Generate Code"):
|
| 59 |
+
if not description.strip():
|
| 60 |
+
st.warning("Please enter a code description!")
|
| 61 |
+
else:
|
| 62 |
+
# Show Progress Bar
|
| 63 |
+
progress_bar = st.progress(0)
|
| 64 |
+
status_text = st.empty()
|
| 65 |
+
|
| 66 |
+
# Progress Simulation
|
| 67 |
+
for i in range(50):
|
| 68 |
+
progress_bar.progress(i + 1)
|
| 69 |
+
status_text.text(f"🔄 Generating code... ({i+1}%)")
|
| 70 |
+
time.sleep(0.03)
|
| 71 |
+
|
| 72 |
+
# Generate Code
|
| 73 |
+
prompt = prompt_template.format(language=language, description=description)
|
| 74 |
+
response = llm.invoke([HumanMessage(content=prompt)])
|
| 75 |
+
|
| 76 |
+
# Progress Complete
|
| 77 |
+
for i in range(50, 100):
|
| 78 |
+
progress_bar.progress(i + 1)
|
| 79 |
+
status_text.text(f"✅ Code Generation Complete ({i+1}%)")
|
| 80 |
+
time.sleep(0.01)
|
| 81 |
+
|
| 82 |
+
# Show Generated Code
|
| 83 |
+
st.subheader("Generated Code:")
|
| 84 |
+
st.code(response.content, language=language.lower())
|
| 85 |
+
|
| 86 |
+
# Save to ChromaDB
|
| 87 |
+
vectorstore.add_texts([response.content])
|
| 88 |
+
|
| 89 |
+
# Run Code Section
|
| 90 |
+
st.subheader("Run Code (For Python, JS, PHP, C#)")
|
| 91 |
+
if st.button("Run Code"):
|
| 92 |
+
if language in ["Python", "JavaScript", "PHP", "C#"]:
|
| 93 |
+
# Save code to temp file
|
| 94 |
+
filename = f"temp_code.{language.lower()}"
|
| 95 |
+
with open(filename, "w") as f:
|
| 96 |
+
f.write(response.content)
|
| 97 |
+
|
| 98 |
+
# Determine execution command
|
| 99 |
+
if language == "Python":
|
| 100 |
+
command = ["python", filename]
|
| 101 |
+
elif language == "JavaScript":
|
| 102 |
+
command = ["node", filename]
|
| 103 |
+
elif language == "PHP":
|
| 104 |
+
command = ["php", filename]
|
| 105 |
+
elif language == "C#":
|
| 106 |
+
command = ["dotnet-script", filename]
|
| 107 |
+
|
| 108 |
+
# Run the code using subprocess
|
| 109 |
+
try:
|
| 110 |
+
output = subprocess.run(command, capture_output=True, text=True, timeout=10)
|
| 111 |
+
st.subheader("Output:")
|
| 112 |
+
st.text(output.stdout if output.stdout else "No output")
|
| 113 |
+
if output.stderr:
|
| 114 |
+
st.subheader("Errors:")
|
| 115 |
+
st.text(output.stderr)
|
| 116 |
+
except Exception as e:
|
| 117 |
+
st.error(f"Error executing code: {str(e)}")
|
| 118 |
+
|
| 119 |
+
# Render HTML/CSS/JS
|
| 120 |
+
if language == "HTML/CSS/JS":
|
| 121 |
+
st.subheader("Live Preview:")
|
| 122 |
+
st.components.v1.html(response.content, height=500)
|
| 123 |
+
|
| 124 |
+
# Download Option
|
| 125 |
+
if st.button("Download Code"):
|
| 126 |
+
with open("generated_code.txt", "w") as f:
|
| 127 |
+
f.write(response.content)
|
| 128 |
+
st.download_button("Download Generated Code", "generated_code.txt", "text/plain")
|
| 129 |
+
|
| 130 |
+
st.markdown("---")
|
| 131 |
+
st.markdown("🔹 **Built with LangChain (Latest), OpenAI GPT, and ChromaDB** 🔹")
|