Spaces:
Runtime error
Runtime error
File size: 3,510 Bytes
d64c4f8 6a516f7 d64c4f8 6a516f7 d64c4f8 6a516f7 d64c4f8 |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import asyncio
from io import BytesIO
from threading import Lock
import gradio as gr
from fastapi import Body, Depends, FastAPI, HTTPException
from PIL import Image
from sqlalchemy import Column, Float, ForeignKey, Integer, String, create_engine
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
from transformers import AutoModelForCausalLM, AutoTokenizer
# Database setup
DATABASE_URL = "sqlite:///./sin_city_rp.db"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
db_lock = Lock()
# Define SQLAlchemy models
class Player(Base):
__tablename__ = "players"
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True)
email = Column(String, unique=True, index=True)
password = Column(String)
characters = relationship("Character", back_populates="player")
class Character(Base):
__tablename__ = "characters"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
player_id = Column(Integer, ForeignKey("players.id"))
player = relationship("Player", back_populates="characters")
level = Column(Integer, default=1)
experience = Column(Integer, default=0)
health = Column(Integer, default=100)
items = relationship("Item", back_populates="character")
class Quest(Base):
__tablename__ = "quests"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
description = Column(String)
reward = Column(Integer)
class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
description = Column(String)
value = Column(Float)
character_id = Column(Integer, ForeignKey("characters.id"))
character = relationship("Character", back_populates="items")
# Create tables
Base.metadata.create_all(engine)
# FastAPI setup
app = FastAPI()
# Dependency for getting the current player
def get_current_player(username: str = Body(...), password: str = Body(...)):
with db_lock:
session = SessionLocal()
player = session.query(Player).filter(Player.username == username, Player.password == password).first()
session.close()
if not player:
raise HTTPException(status_code=401, detail="Invalid username or password")
return player
# Login endpoint
@app.post("/login")
async def login(player: Player = Depends(get_current_player)):
return {"message": f"Welcome {player.username}!"}
# Load the DALL·E Mini model
model_name = "flax-community/dalle-mini"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Function to generate image from text prompt using DALL·E Mini
async def generate_image(prompt: str):
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs)
image = Image.fromarray(outputs[0].numpy())
# Convert the image to a format that can be used in FastAPI/Gradio
buf = BytesIO()
image.save(buf, format='PNG')
buf.seek(0)
return buf
# Gradio Interface
def gradio_interface(prompt):
response = asyncio.run(generate_image(prompt))
return response
interface = gr.Interface(
fn=gradio_interface,
inputs=gr.Textbox(label="Enter prompt"),
outputs=gr.Image(label="Generated Image")
)
if __name__ == "__main__":
interface.launch()
|