Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import uuid
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
+
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
| 5 |
+
from sqlalchemy.orm import sessionmaker, declarative_base
|
| 6 |
+
from sqlalchemy import Column, String, DateTime, func, select
|
| 7 |
+
import asyncio
|
| 8 |
+
|
| 9 |
+
# --------- Banco de dados (Supabase) -------------
|
| 10 |
+
import os
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
DATABASE_URL = os.getenv("DATABASE_URL") # Supabase URL
|
| 15 |
+
engine = create_async_engine(DATABASE_URL, echo=False)
|
| 16 |
+
SessionLocal = sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False)
|
| 17 |
+
Base = declarative_base()
|
| 18 |
+
|
| 19 |
+
class User(Base):
|
| 20 |
+
__tablename__ = "users"
|
| 21 |
+
id = Column(String, primary_key=True)
|
| 22 |
+
name = Column(String)
|
| 23 |
+
api_key = Column(String, unique=True)
|
| 24 |
+
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
| 25 |
+
|
| 26 |
+
async def init_db():
|
| 27 |
+
async with engine.begin() as conn:
|
| 28 |
+
await conn.run_sync(Base.metadata.create_all)
|
| 29 |
+
asyncio.run(init_db())
|
| 30 |
+
|
| 31 |
+
# --------- Modelo Gemma 270M ---------------------
|
| 32 |
+
model_name = "google/gemma-3-270m-it"
|
| 33 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 34 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 35 |
+
gen_pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 36 |
+
|
| 37 |
+
# --------- Funções da API ------------------------
|
| 38 |
+
async def create_user(name: str):
|
| 39 |
+
key = str(uuid.uuid4())
|
| 40 |
+
async with SessionLocal() as session:
|
| 41 |
+
user = User(id=str(uuid.uuid4()), name=name, api_key=key)
|
| 42 |
+
session.add(user)
|
| 43 |
+
await session.commit()
|
| 44 |
+
return key
|
| 45 |
+
|
| 46 |
+
async def check_key(key: str):
|
| 47 |
+
async with SessionLocal() as session:
|
| 48 |
+
result = await session.execute(select(User).where(User.api_key == key))
|
| 49 |
+
user = result.scalar_one_or_none()
|
| 50 |
+
return user is not None
|
| 51 |
+
|
| 52 |
+
def generate_text(prompt, api_key):
|
| 53 |
+
# Verifica chave
|
| 54 |
+
loop = asyncio.get_event_loop()
|
| 55 |
+
valid = loop.run_until_complete(check_key(api_key))
|
| 56 |
+
if not valid:
|
| 57 |
+
return "API Key inválida!"
|
| 58 |
+
result = gen_pipe(prompt, max_length=100, do_sample=True)
|
| 59 |
+
return result[0]["generated_text"]
|
| 60 |
+
|
| 61 |
+
# --------- Interface Gradio ----------------------
|
| 62 |
+
with gr.Blocks() as demo:
|
| 63 |
+
gr.Markdown("## API Gemma 270M - Teste")
|
| 64 |
+
with gr.Tab("Gerar Texto"):
|
| 65 |
+
prompt_input = gr.Textbox(label="Prompt")
|
| 66 |
+
api_key_input = gr.Textbox(label="API Key")
|
| 67 |
+
output_text = gr.Textbox(label="Resultado")
|
| 68 |
+
generate_btn = gr.Button("Gerar")
|
| 69 |
+
generate_btn.click(fn=generate_text, inputs=[prompt_input, api_key_input], outputs=output_text)
|
| 70 |
+
|
| 71 |
+
with gr.Tab("Criar API Key"):
|
| 72 |
+
name_input = gr.Textbox(label="Nome do Usuário")
|
| 73 |
+
key_output = gr.Textbox(label="Sua API Key")
|
| 74 |
+
create_btn = gr.Button("Criar")
|
| 75 |
+
create_btn.click(fn=lambda name: asyncio.get_event_loop().run_until_complete(create_user(name)), inputs=name_input, outputs=key_output)
|
| 76 |
+
|
| 77 |
+
demo.launch()
|