Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,55 @@
|
|
| 1 |
-
|
| 2 |
-
from transformers import
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Load your model (can be any HF model)
|
| 8 |
-
MODEL_NAME = "tiiuae/falcon-7b-instruct"
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
-
MODEL_NAME,
|
| 12 |
-
torch_dtype=torch.bfloat16,
|
| 13 |
-
device_map="auto"
|
| 14 |
-
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
def set_token():
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
-
Handles system + user prompts and returns generated response.
|
| 21 |
"""
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
system_prompt = data.get("system_prompt", "You are a helpful AI.")
|
| 25 |
user_input = data.get("user_input", "")
|
| 26 |
temperature = float(data.get("temperature", 0.7))
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
inputs
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
+
MODEL_NAME = "gpt2"
|
|
|
|
|
|
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def set_token(json_input):
|
|
|
|
| 11 |
"""
|
| 12 |
+
Accepts JSON (string or dict) and returns a JSON response.
|
|
|
|
| 13 |
"""
|
| 14 |
+
try:
|
| 15 |
+
if isinstance(json_input, str):
|
| 16 |
+
data = json.loads(json_input)
|
| 17 |
+
else:
|
| 18 |
+
data = json_input
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return {"error": f"Invalid JSON input: {str(e)}"}
|
| 21 |
|
| 22 |
system_prompt = data.get("system_prompt", "You are a helpful AI.")
|
| 23 |
user_input = data.get("user_input", "")
|
| 24 |
temperature = float(data.get("temperature", 0.7))
|
| 25 |
+
max_tokens = int(data.get("max_tokens", 100))
|
| 26 |
+
|
| 27 |
+
prompt = f"{system_prompt}\nUser: {user_input}\nAI:"
|
| 28 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 29 |
+
outputs = model.generate(
|
| 30 |
+
**inputs,
|
| 31 |
+
max_new_tokens=max_tokens,
|
| 32 |
+
do_sample=True,
|
| 33 |
+
temperature=temperature,
|
| 34 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 35 |
+
)
|
| 36 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
+
if "AI:" in text:
|
| 38 |
+
text = text.split("AI:")[-1].strip()
|
| 39 |
+
|
| 40 |
+
return {
|
| 41 |
+
"model": MODEL_NAME,
|
| 42 |
+
"response": text,
|
| 43 |
+
"tokens_used": len(outputs[0]),
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# Gradio interface
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=set_token,
|
| 49 |
+
inputs=gr.Textbox(label="JSON Request", placeholder='{"system_prompt":"...","user_input":"..."}'),
|
| 50 |
+
outputs="json",
|
| 51 |
+
title="setToken API",
|
| 52 |
+
description="Send a JSON request with system_prompt and user_input. Example: {\"system_prompt\":\"You are helpful.\",\"user_input\":\"Say hi!\"}"
|
| 53 |
+
)
|
| 54 |
|
| 55 |
+
iface.launch()
|
|
|