Spaces:
Runtime error
Runtime error
update.app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 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 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
-
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from io import BytesIO
|
| 3 |
+
from threading import Lock
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
+
from fastapi import Body, Depends, FastAPI, HTTPException
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from sqlalchemy import Column, Float, ForeignKey, Integer, String, create_engine
|
| 9 |
+
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
|
| 10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 11 |
+
|
| 12 |
+
# Database setup
|
| 13 |
+
DATABASE_URL = "sqlite:///./sin_city_rp.db"
|
| 14 |
+
engine = create_engine(DATABASE_URL)
|
| 15 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 16 |
+
Base = declarative_base()
|
| 17 |
+
db_lock = Lock()
|
| 18 |
+
|
| 19 |
+
# Define SQLAlchemy models
|
| 20 |
+
class Player(Base):
|
| 21 |
+
__tablename__ = "players"
|
| 22 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 23 |
+
username = Column(String, unique=True, index=True)
|
| 24 |
+
email = Column(String, unique=True, index=True)
|
| 25 |
+
password = Column(String)
|
| 26 |
+
characters = relationship("Character", back_populates="player")
|
| 27 |
+
|
| 28 |
+
class Character(Base):
|
| 29 |
+
__tablename__ = "characters"
|
| 30 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 31 |
+
name = Column(String, index=True)
|
| 32 |
+
player_id = Column(Integer, ForeignKey("players.id"))
|
| 33 |
+
player = relationship("Player", back_populates="characters")
|
| 34 |
+
level = Column(Integer, default=1)
|
| 35 |
+
experience = Column(Integer, default=0)
|
| 36 |
+
health = Column(Integer, default=100)
|
| 37 |
+
items = relationship("Item", back_populates="character")
|
| 38 |
+
|
| 39 |
+
class Quest(Base):
|
| 40 |
+
__tablename__ = "quests"
|
| 41 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 42 |
+
name = Column(String, index=True)
|
| 43 |
+
description = Column(String)
|
| 44 |
+
reward = Column(Integer)
|
| 45 |
+
|
| 46 |
+
class Item(Base):
|
| 47 |
+
__tablename__ = "items"
|
| 48 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 49 |
+
name = Column(String, index=True)
|
| 50 |
+
description = Column(String)
|
| 51 |
+
value = Column(Float)
|
| 52 |
+
character_id = Column(Integer, ForeignKey("characters.id"))
|
| 53 |
+
character = relationship("Character", back_populates="items")
|
| 54 |
+
|
| 55 |
+
# Create tables
|
| 56 |
+
Base.metadata.create_all(engine)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# FastAPI setup
|
| 59 |
+
app = FastAPI()
|
| 60 |
+
|
| 61 |
+
# Dependency for getting the current player
|
| 62 |
+
def get_current_player(username: str = Body(...), password: str = Body(...)):
|
| 63 |
+
with db_lock:
|
| 64 |
+
session = SessionLocal()
|
| 65 |
+
player = session.query(Player).filter(Player.username == username, Player.password == password).first()
|
| 66 |
+
session.close()
|
| 67 |
+
if not player:
|
| 68 |
+
raise HTTPException(status_code=401, detail="Invalid username or password")
|
| 69 |
+
return player
|
| 70 |
+
|
| 71 |
+
# Login endpoint
|
| 72 |
+
@app.post("/login")
|
| 73 |
+
async def login(player: Player = Depends(get_current_player)):
|
| 74 |
+
return {"message": f"Welcome {player.username}!"}
|
| 75 |
+
|
| 76 |
+
# Load the DALL路E Mini model
|
| 77 |
+
model_name = "flax-community/dalle-mini"
|
| 78 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 79 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 80 |
+
|
| 81 |
+
# Function to generate image from text prompt using DALL路E Mini
|
| 82 |
+
async def generate_image(prompt: str):
|
| 83 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 84 |
+
outputs = model.generate(**inputs)
|
| 85 |
+
image = Image.fromarray(outputs[0].numpy())
|
| 86 |
+
|
| 87 |
+
# Convert the image to a format that can be used in FastAPI/Gradio
|
| 88 |
+
buf = BytesIO()
|
| 89 |
+
image.save(buf, format='PNG')
|
| 90 |
+
buf.seek(0)
|
| 91 |
+
return buf
|
| 92 |
+
|
| 93 |
+
# Gradio Interface
|
| 94 |
+
def gradio_interface(prompt):
|
| 95 |
+
response = asyncio.run(generate_image(prompt))
|
| 96 |
+
return response
|
| 97 |
+
|
| 98 |
+
interface = gr.Interface(
|
| 99 |
+
fn=gradio_interface,
|
| 100 |
+
inputs=gr.Textbox(label="Enter prompt"),
|
| 101 |
+
outputs=gr.Image(label="Generated Image")
|
| 102 |
+
)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
+
interface.launch()
|