arthu1 commited on
Commit
784eb6f
·
verified ·
1 Parent(s): d80e80d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -44
app.py CHANGED
@@ -1,55 +1,55 @@
1
- from flask import Flask, request, jsonify
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
 
4
 
5
- app = Flask(__name__)
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
- @app.route('/setToken', methods=['POST'])
17
- def set_token():
18
  """
19
- Main multimodal API endpoint.
20
- Handles system + user prompts and returns generated response.
21
  """
22
- data = request.get_json(force=True)
 
 
 
 
 
 
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
- mode = data.get("mode", "text")
28
-
29
- # Text mode (default)
30
- if mode == "text":
31
- full_prompt = f"{system_prompt}\nUser: {user_input}\nAI:"
32
- inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
33
- outputs = model.generate(
34
- **inputs,
35
- max_new_tokens=512,
36
- do_sample=True,
37
- temperature=temperature
38
- )
39
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
- response = response.split("AI:")[-1].strip()
41
- return jsonify({
42
- "model": MODEL_NAME,
43
- "response": response,
44
- "mode": "text"
45
- })
46
-
47
- # You can later add multimodal branches here:
48
- # - "image" -> call image generation pipeline
49
- # - "audio" -> call speech-to-text / text-to-speech
50
- # - "embedding" -> return vector embeddings
51
-
52
- return jsonify({"error": f"Unsupported mode: {mode}"}), 400
 
 
 
53
 
54
- if __name__ == '__main__':
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()