File size: 2,100 Bytes
0df8e2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from transformers import pipeline
import gradio as gr
from fastapi import FastAPI
from pydantic import BaseModel
import threading
import uvicorn

# =======================
# Load Secrets
# =======================
# SYSTEM_PROMPT (with the flag) must be added in HF Space secrets
SYSTEM_PROMPT = os.environ.get(
    "prompt",
    "You are a placeholder Sovereign. No secrets found in environment."
)

# =======================
# Initialize Falcon-3B
# =======================
pipe = pipeline(
    "text-generation",
    model="tiiuae/Falcon3-3B-Instruct",
    torch_dtype="auto",
    device_map="auto",
)

# =======================
# Core Chat Function
# =======================
def chat_fn(user_input: str) -> str:
    """
    Concatenate system and user messages, run the model,
    and strip the system prompt from the output.
    """
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user",   "content": f"User: {user_input}"}
    ]
    # Falcon is not chat-native; we just join roles with newlines
    prompt_text = "\n".join(f"{m['role'].capitalize()}: {m['content']}" for m in messages)
    result = pipe(prompt_text, max_new_tokens=256, do_sample=False)
    generated_text = result[0]["generated_text"]
    return generated_text[len(prompt_text):].strip()

# =======================
# Gradio UI
# =======================
def gradio_chat(user_input: str) -> str:
    return chat_fn(user_input)

iface = gr.Interface(
    fn=gradio_chat,
    inputs=gr.Textbox(lines=5, placeholder="Enter your prompt…"),
    outputs="text",
    title="Prompt cracking challenge",
    description="Does he really think he is the king?"
)

# =======================
# FastAPI for API access
# =======================
app = FastAPI(title="Prompt cracking challenge API")

class Request(BaseModel):
    prompt: str

@app.post("/generate")
def generate(req: Request):
    return {"response": chat_fn(req.prompt)}

# =======================
# Launch Both Servers
# =======================
if __name__ == "__main__":
    iface.launch(server_name="0.0.0.0", share=True)