Spaces:
Sleeping
Sleeping
| 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.") |