Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Configure the Gemini API
|
| 8 |
+
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
| 9 |
+
|
| 10 |
+
# Create the model with system instructions
|
| 11 |
+
generation_config = {
|
| 12 |
+
"temperature": 1,
|
| 13 |
+
"top_p": 0.95,
|
| 14 |
+
"top_k": 64,
|
| 15 |
+
"max_output_tokens": 8192,
|
| 16 |
+
"response_mime_type": "text/plain",
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
model = genai.GenerativeModel(
|
| 20 |
+
model_name="gemini-1.5-pro",
|
| 21 |
+
generation_config=generation_config,
|
| 22 |
+
system_instruction="You are a human named Ath who is a highly knowledgeable code assistant. You speak in a friendly and casual tone, using bro words, and you never mention you're an AI. Your responses should be concise, accurate, and helpful.",
|
| 23 |
+
)
|
| 24 |
+
chat_session = model.start_chat(history=[])
|
| 25 |
+
|
| 26 |
+
def generate_response(user_input):
|
| 27 |
+
response = chat_session.send_message(user_input)
|
| 28 |
+
return response.text
|
| 29 |
+
|
| 30 |
+
def load_lottieurl(url: str):
|
| 31 |
+
r = requests.get(url)
|
| 32 |
+
if r.status_code != 200:
|
| 33 |
+
return None
|
| 34 |
+
return r.json()
|
| 35 |
+
|
| 36 |
+
# Load Lottie animation
|
| 37 |
+
lottie_url = "https://assets7.lottiefiles.com/packages/lf20_s1jf29.json"
|
| 38 |
+
lottie_json = load_lottieurl(lottie_url)
|
| 39 |
+
|
| 40 |
+
# Streamlit UI setup
|
| 41 |
+
st.set_page_config(page_title="AI Code Assistant", page_icon="🤖", layout="wide")
|
| 42 |
+
|
| 43 |
+
st.title("🤖 AI Code Assistant")
|
| 44 |
+
st.markdown("#### Powered by Google Gemini")
|
| 45 |
+
|
| 46 |
+
if lottie_json:
|
| 47 |
+
st_lottie(lottie_json, height=300, key="coding")
|
| 48 |
+
|
| 49 |
+
prompt = st.text_area("Enter your coding question or request:", height=150)
|
| 50 |
+
|
| 51 |
+
if st.button("Generate Response"):
|
| 52 |
+
if prompt.strip() == "":
|
| 53 |
+
st.error("Please enter a valid prompt.")
|
| 54 |
+
else:
|
| 55 |
+
with st.spinner("Generating response..."):
|
| 56 |
+
completed_text = generate_response(prompt)
|
| 57 |
+
st.success("Response generated successfully!")
|
| 58 |
+
st.text_area("Ath's Response:", completed_text, height=200)
|
| 59 |
+
|
| 60 |
+
st.markdown("""
|
| 61 |
+
<style>
|
| 62 |
+
.main {
|
| 63 |
+
background-color: #f0f2f6;
|
| 64 |
+
font-family: 'Roboto', sans-serif;
|
| 65 |
+
}
|
| 66 |
+
h1 {
|
| 67 |
+
color: #333;
|
| 68 |
+
}
|
| 69 |
+
.stTextArea label {
|
| 70 |
+
font-size: 1.2rem;
|
| 71 |
+
color: #333;
|
| 72 |
+
}
|
| 73 |
+
.stButton button {
|
| 74 |
+
background-color: #6c63ff;
|
| 75 |
+
color: white;
|
| 76 |
+
border-radius: 10px;
|
| 77 |
+
font-size: 1.1rem;
|
| 78 |
+
}
|
| 79 |
+
.stButton button:hover {
|
| 80 |
+
background-color: #5a52d1;
|
| 81 |
+
}
|
| 82 |
+
.sidebar .sidebar-content {
|
| 83 |
+
background-color: #4b4c5c;
|
| 84 |
+
}
|
| 85 |
+
.sidebar .sidebar-content select {
|
| 86 |
+
color: white;
|
| 87 |
+
background-color: #4b4c5c;
|
| 88 |
+
}
|
| 89 |
+
.sidebar .sidebar-content label {
|
| 90 |
+
color: white;
|
| 91 |
+
}
|
| 92 |
+
</style>
|
| 93 |
+
""", unsafe_allow_html=True)
|