ChainGPT-Solidity-LLM / src /streamlit_app.py
muhammad-mujtaba-ai's picture
Upload folder using huggingface_hub
6f37b9b verified
import streamlit as st
import requests
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
st.set_page_config(page_title="Solidity Code Generator")
st.title("πŸ› οΈ Solidity Code Generator")
st.write("Enter a coding instruction and get Solidity code")
prompt = st.text_area("πŸ’¬ Instruction", height=150, max_chars=1000, help="Keep your instruction clear and concise (max 1000 characters)")
if st.button("πŸš€ Generate Code"):
if not prompt.strip():
st.warning("Please enter a valid instruction.")
elif len(prompt) > 1000: # Limit prompt length to prevent model crashes
st.warning("⚠️ Your instruction is too long. Please keep it under 1000 characters for better results.")
else:
with st.spinner("Generating..."):
try:
# Get the API URL and authorization token from environment variables
api_url = os.getenv("API_URL")
auth_token = os.getenv("AUTH_TOKEN")
# Check if required environment variables are set
if not api_url:
st.error("❌ Configuration error: API URL not found. Please set the API_URL environment variable.")
st.stop()
if not auth_token:
st.error("❌ Configuration error: Authentication token not found. Please set the AUTH_TOKEN environment variable.")
st.stop()
headers = {
"Authorization": f"Bearer {auth_token}"
}
response = requests.post(api_url, json={"prompt": prompt}, stream=True, headers=headers)
response.raise_for_status()
# Get only the code from the JSON response
result = response.json()
solidity_code = result.get("code", "")
if solidity_code:
st.code(solidity_code, language="solidity")
else:
st.warning("Received an empty response from the Deployed Model.")
except requests.exceptions.Timeout:
st.error("❌ Request timed out. The service is taking too long to respond. Please try again.")
except requests.exceptions.ConnectionError:
st.error("❌ Network error: Unable to connect to the service. Please check your internet connection.")
except requests.exceptions.HTTPError as e:
if e.response.status_code == 503:
st.error("❌ Service temporarily unavailable. The AI service is currently down. Please try again later.")
elif e.response.status_code == 401:
st.error("❌ Authentication failed. Please check your API token.")
elif e.response.status_code == 403:
st.error("❌ Access denied. Please check your API permissions.")
else:
st.error(f"❌ HTTP Error {e.response.status_code}: {e.response.text}")
except requests.exceptions.RequestException as e:
st.error("❌ Network error: Unable to connect to the service. Please try again later.")
except KeyError:
st.error("❌ Configuration error: Required environment variables not found.")
except Exception as e:
st.error("❌ An unexpected error occurred. Please try again later.")