File size: 3,455 Bytes
a21d3d0
7d4b3f6
 
 
 
 
 
 
 
 
 
 
 
6f37b9b
7d4b3f6
 
 
 
6f37b9b
 
7d4b3f6
 
 
6f37b9b
 
7d4b3f6
6f37b9b
 
 
 
 
 
 
 
7d4b3f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f37b9b
7d4b3f6
 
1
2
3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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.")