Update app.py
Browse files
app.py
CHANGED
|
@@ -7,16 +7,14 @@ import os
|
|
| 7 |
import uuid
|
| 8 |
import hashlib
|
| 9 |
import json
|
| 10 |
-
import
|
| 11 |
-
import time
|
| 12 |
-
from datetime import datetime, timedelta
|
| 13 |
|
| 14 |
import streamlit as st
|
| 15 |
import requests
|
| 16 |
|
| 17 |
from sqlalchemy import create_engine, Column, Integer, String, Float, Boolean, DateTime, ForeignKey
|
| 18 |
from sqlalchemy.ext.declarative import declarative_base
|
| 19 |
-
from sqlalchemy.orm import sessionmaker
|
| 20 |
|
| 21 |
import openai
|
| 22 |
|
|
@@ -146,7 +144,7 @@ session = Session()
|
|
| 146 |
def mint_tokens_via_api(address, token, balance):
|
| 147 |
"""
|
| 148 |
Mints tokens by calling the external API endpoint.
|
| 149 |
-
Returns
|
| 150 |
"""
|
| 151 |
url = 'https://backend.buildbear.io/node/faucet/erc20/linguistic-doctorstrange-fb0930a4'
|
| 152 |
headers = {
|
|
@@ -174,23 +172,12 @@ def mint_tokens_via_api(address, token, balance):
|
|
| 174 |
}
|
| 175 |
try:
|
| 176 |
response = requests.post(url, headers=headers, json=payload)
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
# Check if the response contains content
|
| 181 |
-
if response.content:
|
| 182 |
-
try:
|
| 183 |
-
response_data = response.json()
|
| 184 |
-
return True, response_data
|
| 185 |
-
except ValueError:
|
| 186 |
-
st.error(f"β Invalid JSON response: {response.text}")
|
| 187 |
-
return False, f"Invalid JSON response: {response.text}"
|
| 188 |
else:
|
| 189 |
-
|
| 190 |
-
return False, "Empty response from the API."
|
| 191 |
except requests.exceptions.RequestException as e:
|
| 192 |
-
|
| 193 |
-
return False, f"Request failed: {e}"
|
| 194 |
|
| 195 |
def calculate_token_grade(ai_output):
|
| 196 |
"""
|
|
@@ -317,7 +304,6 @@ if openai_enabled:
|
|
| 317 |
mint_success, mint_response = mint_tokens_via_api(token_address, token_contract, balance)
|
| 318 |
if mint_success:
|
| 319 |
st.success("β
Token minted successfully via API!")
|
| 320 |
-
st.write(f"**API Response:** {json.dumps(mint_response, indent=2)}")
|
| 321 |
# Update local database
|
| 322 |
token = Token(
|
| 323 |
id=str(uuid.uuid4()),
|
|
@@ -335,13 +321,13 @@ if openai_enabled:
|
|
| 335 |
transaction = Transaction(
|
| 336 |
token_id=token.id,
|
| 337 |
action='minted',
|
| 338 |
-
value=
|
| 339 |
hash=token.verification_hash
|
| 340 |
)
|
| 341 |
session.add(transaction)
|
| 342 |
session.commit()
|
| 343 |
st.write(f"**Token ID:** {token.id}")
|
| 344 |
-
st.write(f"**Tokens Earned:** {
|
| 345 |
st.write(f"**Stored Text:** {token.text}")
|
| 346 |
else:
|
| 347 |
st.error(f"β Token minting failed: {mint_response}")
|
|
@@ -580,8 +566,6 @@ if mint_button:
|
|
| 580 |
|
| 581 |
if mint_success:
|
| 582 |
st.sidebar.success("β
Token minted successfully via API!")
|
| 583 |
-
st.sidebar.write(f"**API Response:** {json.dumps(mint_response, indent=2)}")
|
| 584 |
-
|
| 585 |
# Update local database
|
| 586 |
token = Token(
|
| 587 |
id=str(uuid.uuid4()),
|
|
|
|
| 7 |
import uuid
|
| 8 |
import hashlib
|
| 9 |
import json
|
| 10 |
+
from datetime import datetime
|
|
|
|
|
|
|
| 11 |
|
| 12 |
import streamlit as st
|
| 13 |
import requests
|
| 14 |
|
| 15 |
from sqlalchemy import create_engine, Column, Integer, String, Float, Boolean, DateTime, ForeignKey
|
| 16 |
from sqlalchemy.ext.declarative import declarative_base
|
| 17 |
+
from sqlalchemy.orm import sessionmaker
|
| 18 |
|
| 19 |
import openai
|
| 20 |
|
|
|
|
| 144 |
def mint_tokens_via_api(address, token, balance):
|
| 145 |
"""
|
| 146 |
Mints tokens by calling the external API endpoint.
|
| 147 |
+
Returns True if successful, False otherwise.
|
| 148 |
"""
|
| 149 |
url = 'https://backend.buildbear.io/node/faucet/erc20/linguistic-doctorstrange-fb0930a4'
|
| 150 |
headers = {
|
|
|
|
| 172 |
}
|
| 173 |
try:
|
| 174 |
response = requests.post(url, headers=headers, json=payload)
|
| 175 |
+
if response.status_code == 200:
|
| 176 |
+
return True, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
else:
|
| 178 |
+
return False, response.text
|
|
|
|
| 179 |
except requests.exceptions.RequestException as e:
|
| 180 |
+
return False, str(e)
|
|
|
|
| 181 |
|
| 182 |
def calculate_token_grade(ai_output):
|
| 183 |
"""
|
|
|
|
| 304 |
mint_success, mint_response = mint_tokens_via_api(token_address, token_contract, balance)
|
| 305 |
if mint_success:
|
| 306 |
st.success("β
Token minted successfully via API!")
|
|
|
|
| 307 |
# Update local database
|
| 308 |
token = Token(
|
| 309 |
id=str(uuid.uuid4()),
|
|
|
|
| 321 |
transaction = Transaction(
|
| 322 |
token_id=token.id,
|
| 323 |
action='minted',
|
| 324 |
+
value=token.value,
|
| 325 |
hash=token.verification_hash
|
| 326 |
)
|
| 327 |
session.add(transaction)
|
| 328 |
session.commit()
|
| 329 |
st.write(f"**Token ID:** {token.id}")
|
| 330 |
+
st.write(f"**Tokens Earned:** {token.value:.2f}")
|
| 331 |
st.write(f"**Stored Text:** {token.text}")
|
| 332 |
else:
|
| 333 |
st.error(f"β Token minting failed: {mint_response}")
|
|
|
|
| 566 |
|
| 567 |
if mint_success:
|
| 568 |
st.sidebar.success("β
Token minted successfully via API!")
|
|
|
|
|
|
|
| 569 |
# Update local database
|
| 570 |
token = Token(
|
| 571 |
id=str(uuid.uuid4()),
|