Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
+
|
| 10 |
+
# Initialize Groq client
|
| 11 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
+
|
| 13 |
+
# Streamlit UI
|
| 14 |
+
st.title("AI Code Generator & Debugger")
|
| 15 |
+
st.write("Generate and debug code in multiple programming languages.")
|
| 16 |
+
|
| 17 |
+
language = st.selectbox("Select Programming Language", ["Python", "Java", "JavaScript", "PHP", "C++", "C#"])
|
| 18 |
+
mode = st.radio("Select Mode", ["Generate Code", "Debug Code"])
|
| 19 |
+
user_input = st.text_area("Enter your prompt or code snippet")
|
| 20 |
+
submit = st.button("Submit")
|
| 21 |
+
|
| 22 |
+
def get_groq_response(prompt):
|
| 23 |
+
completion = client.chat.completions.create(
|
| 24 |
+
model="deepseek-r1-distill-llama-70b",
|
| 25 |
+
messages=[{"role": "system", "content": "You are an expert AI code assistant."},
|
| 26 |
+
{"role": "user", "content": prompt}],
|
| 27 |
+
temperature=0.6,
|
| 28 |
+
max_completion_tokens=4096,
|
| 29 |
+
top_p=0.95,
|
| 30 |
+
stream=True,
|
| 31 |
+
stop=None,
|
| 32 |
+
)
|
| 33 |
+
return "".join(chunk.choices[0].delta.content or "" for chunk in completion)
|
| 34 |
+
|
| 35 |
+
if submit:
|
| 36 |
+
if user_input.strip():
|
| 37 |
+
if any(lang.lower() in user_input.lower() for lang in ["python", "java", "javascript", "php", "c++", "c#"]):
|
| 38 |
+
prompt = f"{mode} {language} code: {user_input}"
|
| 39 |
+
response = get_groq_response(prompt)
|
| 40 |
+
st.code(response, language=language.lower())
|
| 41 |
+
else:
|
| 42 |
+
st.error("Invalid question. Please try again with a relevant coding prompt.")
|
| 43 |
+
else:
|
| 44 |
+
st.warning("Please enter a prompt or code snippet.")
|