Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Title of the application
|
| 5 |
+
st.title("AI Code Bot")
|
| 6 |
+
|
| 7 |
+
# Introduction
|
| 8 |
+
st.write("""
|
| 9 |
+
### Generate Code Snippets with AI
|
| 10 |
+
Enter a programming-related prompt, and the AI will generate a code snippet for you.
|
| 11 |
+
""")
|
| 12 |
+
|
| 13 |
+
# Input box for user query
|
| 14 |
+
user_input = st.text_area("Enter your prompt (e.g., 'Write a Python function to reverse a list'):")
|
| 15 |
+
|
| 16 |
+
# Load the Hugging Face pipeline (code generation)
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_model():
|
| 19 |
+
# Authenticate automatically in Hugging Face Spaces
|
| 20 |
+
model = pipeline(
|
| 21 |
+
"text-generation",
|
| 22 |
+
model="EleutherAI/gpt-neo-1.3B" # The model to use
|
| 23 |
+
)
|
| 24 |
+
return model
|
| 25 |
+
|
| 26 |
+
model = load_model()
|
| 27 |
+
|
| 28 |
+
# When the user clicks the button
|
| 29 |
+
if st.button("Generate Code"):
|
| 30 |
+
if user_input.strip() == "":
|
| 31 |
+
st.warning("Please enter a prompt.")
|
| 32 |
+
else:
|
| 33 |
+
with st.spinner("Generating code..."):
|
| 34 |
+
try:
|
| 35 |
+
# Generate code using the model
|
| 36 |
+
result = model(user_input, max_length=100, num_return_sequences=1)
|
| 37 |
+
generated_code = result[0]["generated_text"]
|
| 38 |
+
|
| 39 |
+
# Display the output
|
| 40 |
+
st.code(generated_code, language="python")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(f"An error occurred: {e}")
|