Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException, Depends | |
| from pydantic import BaseModel | |
| from web3 import Web3 | |
| import os | |
| # Pydantic model for input validation | |
| class TransferRequest(BaseModel): | |
| recipient_address: str | |
| execute_transfer: bool = False # Default to False | |
| class Validation(BaseModel): | |
| prompt: str | |
| # FastAPI app | |
| app = FastAPI() | |
| # Global counter | |
| counter = 0 | |
| # Modified increment function | |
| def increment_and_print(value: int): | |
| global counter | |
| counter += value | |
| print(f"Counter: {counter}") | |
| return counter | |
| # Web3 setup | |
| BASE_RPC_URL = "https://mainnet.base.org" # Replace with testnet URL if needed | |
| w3 = Web3(Web3.HTTPProvider(BASE_RPC_URL)) | |
| # Function to get the private key from Hugging Face Secrets | |
| def get_private_key(): | |
| private_key = os.environ.get("LumKey") | |
| if not private_key: | |
| raise HTTPException(status_code=500, detail="Private key not found in secrets.") | |
| return private_key | |
| # Function to perform the ETH transfer | |
| def transfer_eth(recipient_address: str, private_key: str): | |
| try: | |
| account = w3.eth.account.from_key(private_key) | |
| sender_address = account.address | |
| nonce = w3.eth.get_transaction_count(sender_address) | |
| tx = { | |
| 'nonce': nonce, | |
| 'to': w3.to_checksum_address(recipient_address), | |
| 'value': w3.to_wei(0.001, 'ether'), # Changed to 0.001 ETH | |
| 'gas': 21000, # Standard ETH transfer gas | |
| 'gasPrice': w3.eth.gas_price, | |
| 'chainId': 8453, #Base mainnet chainID | |
| } | |
| signed_tx = account.sign_transaction(tx) | |
| tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction) | |
| return w3.to_hex(tx_hash) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Transaction failed: {str(e)}") | |
| # Zephyr completion (existing endpoint - unchanged) | |
| async def stream(item: Validation): | |
| system_prompt = 'Below is an instruction that describes a task. Write a response that appropriately completes the request.' | |
| E_INST = "</s>" | |
| user, assistant = "<|user|>", "<|assistant|>" | |
| prompt = f"{system_prompt}{E_INST}\n{user}\n{item.prompt.strip()}{E_INST}\n{assistant}\n" | |
| return llm(prompt) | |
| # New endpoint for incrementing with prompt value | |
| async def increment_from_prompt(item: Validation): | |
| match = re.search(r'\d+', item.prompt) | |
| if match: | |
| increment_value = int(match.group()) | |
| result = increment_and_print(increment_value) | |
| else: | |
| result = increment_and_print(0) | |
| return {"counter": result} | |
| # New endpoint for simple increment | |
| async def increment(): | |
| result = increment_and_print(1) | |
| return {"counter": result} | |
| # API endpoint for ETH transfer (renamed to /reward) | |
| # @app.post("/reward") | |
| # async def reward_endpoint(request: TransferRequest, private_key: str = Depends(get_private_key)): | |
| # if request.execute_transfer: | |
| # try: | |
| # tx_hash = transfer_eth(request.recipient_address, private_key) | |
| # return {"transaction_hash": tx_hash} | |
| # except HTTPException as e: | |
| # raise e | |
| # except Exception as e: | |
| # raise HTTPException(status_code=500, detail=str(e)) | |
| # else: | |
| # return {"message": "Transaction blocked by execute_transfer flag."} |