Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,138 +1,113 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
from groq import Groq
|
| 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 |
-
return response
|
| 40 |
-
except Exception as e:
|
| 41 |
-
return f"β οΈ Hugging Face Error: {e}"
|
| 42 |
-
|
| 43 |
-
def query_groq(model, prompt):
|
| 44 |
-
try:
|
| 45 |
-
client = Groq(api_key=GROQ_API_KEY)
|
| 46 |
-
resp = client.chat.completions.create(
|
| 47 |
-
model=model,
|
| 48 |
-
messages=[{"role": "user", "content": prompt}],
|
| 49 |
-
temperature=0.7,
|
| 50 |
-
max_tokens=600
|
| 51 |
-
)
|
| 52 |
-
return resp.choices[0].message.content
|
| 53 |
-
except Exception as e:
|
| 54 |
-
return f"β οΈ Groq Error: {e}"
|
| 55 |
-
|
| 56 |
-
# =======================
|
| 57 |
-
# Streamlit UI
|
| 58 |
-
# =======================
|
| 59 |
-
st.set_page_config(page_title="CodeCraft AI", page_icon="β‘", layout="wide")
|
| 60 |
-
st.title("β‘ CodeCraft AI β Mini Copilot for Hackathons")
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
model_choice = st.selectbox("Select Groq Model", GROQ_MODELS)
|
| 70 |
-
|
| 71 |
-
tabs = st.tabs(["π» Generate Code", "π Debug Code", "π Explain Code"])
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
if st.button("π Generate Code"):
|
| 79 |
-
if not prompt.strip():
|
| 80 |
-
st.warning("Please enter something.")
|
| 81 |
else:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
if "```" in output:
|
| 110 |
-
try:
|
| 111 |
-
corrected = output.split("```")[1].replace("python", "").strip()
|
| 112 |
-
explanation = output.split("```")[-1].strip()
|
| 113 |
-
st.subheader("β
Corrected Code")
|
| 114 |
-
st.code(corrected, language="python")
|
| 115 |
-
st.subheader("π Explanation")
|
| 116 |
-
st.write(explanation)
|
| 117 |
-
except:
|
| 118 |
-
st.write(output)
|
| 119 |
else:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
else:
|
| 131 |
-
|
| 132 |
-
with st.spinner("AI is explaining..."):
|
| 133 |
-
output = query_hf(model_choice, explain_prompt) if backend == "Hugging Face" else query_groq(model_choice, explain_prompt)
|
| 134 |
-
st.subheader("π Explanation")
|
| 135 |
-
st.write(output)
|
| 136 |
|
| 137 |
|
| 138 |
# =======================
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
|
| 4 |
+
# Initialize Groq client
|
| 5 |
+
client = Groq(api_key=st.secrets["GROQ_API_KEY"])
|
| 6 |
+
|
| 7 |
+
# Sidebar for model selection
|
| 8 |
+
st.sidebar.title("βοΈ Settings")
|
| 9 |
+
model_choice = st.sidebar.selectbox(
|
| 10 |
+
"Choose AI Model",
|
| 11 |
+
["llama-3.1-8b-instant", "llama-3.1-70b-versatile", "mixtral-8x7b-32768"]
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
st.title("π€ CodeCraft AI - Mini Copilot")
|
| 15 |
+
|
| 16 |
+
# Tabs
|
| 17 |
+
tab1, tab2, tab3 = st.tabs(["π‘ Generate Code", "π Debug Code", "π Explain Code"])
|
| 18 |
+
|
| 19 |
+
# ------------------------- Generate Code -------------------------
|
| 20 |
+
with tab1:
|
| 21 |
+
st.subheader("π‘ Generate Code from Statement")
|
| 22 |
+
statement = st.text_area("Enter your problem statement (e.g., 'Build a calculator in Python')")
|
| 23 |
+
|
| 24 |
+
if st.button("Generate Code"):
|
| 25 |
+
if statement.strip():
|
| 26 |
+
with st.spinner("Generating code..."):
|
| 27 |
+
response = client.chat.completions.create(
|
| 28 |
+
model=model_choice,
|
| 29 |
+
messages=[
|
| 30 |
+
{"role": "system", "content": "You are a helpful coding assistant."},
|
| 31 |
+
{"role": "user", "content": f"Generate correct code for: {statement}. "
|
| 32 |
+
f"First show the code block clearly. "
|
| 33 |
+
f"Then give a short simple explanation."}
|
| 34 |
+
],
|
| 35 |
+
temperature=0.3
|
| 36 |
+
)
|
| 37 |
+
result = response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Split into code + explanation
|
| 40 |
+
if "```" in result:
|
| 41 |
+
parts = result.split("```")
|
| 42 |
+
code = parts[1] if len(parts) > 1 else ""
|
| 43 |
+
explanation = "".join(parts[2:]) if len(parts) > 2 else result
|
| 44 |
+
else:
|
| 45 |
+
code, explanation = result, ""
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Show code (with copy button provided by Streamlit automatically)
|
| 48 |
+
st.code(code, language="python")
|
| 49 |
+
# Show explanation (plain text, no copy)
|
| 50 |
+
st.markdown("### π Explanation")
|
| 51 |
+
st.write(explanation.strip())
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
+
st.warning("β οΈ Please enter a statement.")
|
| 54 |
+
|
| 55 |
+
# ------------------------- Debug Code -------------------------
|
| 56 |
+
with tab2:
|
| 57 |
+
st.subheader("π Debug Your Code")
|
| 58 |
+
user_code = st.text_area("Paste your buggy code here")
|
| 59 |
+
|
| 60 |
+
if st.button("Debug Code"):
|
| 61 |
+
if user_code.strip():
|
| 62 |
+
with st.spinner("Analyzing and fixing your code..."):
|
| 63 |
+
response = client.chat.completions.create(
|
| 64 |
+
model=model_choice,
|
| 65 |
+
messages=[
|
| 66 |
+
{"role": "system", "content": "You are an expert code debugger."},
|
| 67 |
+
{"role": "user", "content": f"Here is code: {user_code}. "
|
| 68 |
+
f"Fix errors and give corrected code. "
|
| 69 |
+
f"Then explain simply what I needed to change and why. "
|
| 70 |
+
f"Do not use 'fix 1, fix 2' format, just a simple explanation."}
|
| 71 |
+
],
|
| 72 |
+
temperature=0.2
|
| 73 |
+
)
|
| 74 |
+
result = response.choices[0].message.content
|
| 75 |
+
|
| 76 |
+
if "```" in result:
|
| 77 |
+
parts = result.split("```")
|
| 78 |
+
code = parts[1] if len(parts) > 1 else ""
|
| 79 |
+
explanation = "".join(parts[2:]) if len(parts) > 2 else result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
else:
|
| 81 |
+
code, explanation = result, ""
|
| 82 |
+
|
| 83 |
+
st.code(code, language="python")
|
| 84 |
+
st.markdown("### π Explanation of Fixes")
|
| 85 |
+
st.write(explanation.strip())
|
| 86 |
+
else:
|
| 87 |
+
st.warning("β οΈ Please paste your code.")
|
| 88 |
+
|
| 89 |
+
# ------------------------- Explain Code -------------------------
|
| 90 |
+
with tab3:
|
| 91 |
+
st.subheader("π Explain Your Code")
|
| 92 |
+
code_to_explain = st.text_area("Paste your code to understand it")
|
| 93 |
+
|
| 94 |
+
if st.button("Explain Code"):
|
| 95 |
+
if code_to_explain.strip():
|
| 96 |
+
with st.spinner("Explaining your code..."):
|
| 97 |
+
response = client.chat.completions.create(
|
| 98 |
+
model=model_choice,
|
| 99 |
+
messages=[
|
| 100 |
+
{"role": "system", "content": "You are a teacher that explains code in simple terms."},
|
| 101 |
+
{"role": "user", "content": f"Explain this code in very simple words:\n{code_to_explain}"}
|
| 102 |
+
],
|
| 103 |
+
temperature=0.5
|
| 104 |
+
)
|
| 105 |
+
explanation = response.choices[0].message.content
|
| 106 |
+
|
| 107 |
+
st.markdown("### π Explanation")
|
| 108 |
+
st.write(explanation.strip())
|
| 109 |
else:
|
| 110 |
+
st.warning("β οΈ Please paste your code.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
|
| 113 |
# =======================
|