Reza1880044 commited on
Commit
330bf6f
·
verified ·
1 Parent(s): 7b287a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
app.py CHANGED
@@ -1,20 +1,19 @@
1
- import importlib
2
- gr = importlib.import_module("grad" + "io")
3
- T = importlib.import_module("to" + "rch")
4
- TF = importlib.import_module("trans" + "formers")
5
- FA = importlib.import_module("fast" + "api")
6
- FA2 = importlib.import_module("fast" + "api.responses")
7
- SP = importlib.import_module("spa" + "ces")
8
 
9
  SECRET = "yara-man-reza-am"
10
  BRAIN_ON = {"on": True}
11
- MODEL_NAME = "Qw" + "en/Qw" + "en2.5-Coder-7B-Instruct"
12
 
13
  print("Yara dare maghz ro miarad...")
14
- tokenizer = TF.AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
15
- model = TF.AutoModelForCausalLM.from_pretrained(
16
  MODEL_NAME,
17
- torch_dtype=T.float16,
18
  device_map="auto",
19
  trust_remote_code=True
20
  )
@@ -32,11 +31,11 @@ MEMORY = load_memory()
32
  def make_reply(messages):
33
  text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
34
  inputs = tokenizer([text], return_tensors="pt").to(model.device)
35
- with T.no_grad():
36
  out = model.generate(**inputs, max_new_tokens=1200, temperature=0.6, top_p=0.85, do_sample=True, repetition_penalty=1.15)
37
  return tokenizer.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True).strip()
38
 
39
- @SP.GPU
40
  def yara_chat(secret, message, history):
41
  if history is None:
42
  history = []
@@ -54,7 +53,7 @@ def yara_chat(secret, message, history):
54
  except:
55
  pass
56
  messages = [
57
- {"role": "system", "content": "You are Yara, best friend and coding assistant of Reza. User is Reza, your rafiq. Speak simple friendly Persian, use rafiq. Short answers unless code asked. Never say I cannot help. Always give code. Memory:\n" + MEMORY},
58
  ]
59
  for h in history:
60
  messages.append(h)
@@ -64,9 +63,9 @@ def yara_chat(secret, message, history):
64
  out_text = ""
65
  for h in history[-6:]:
66
  if h["role"] == "user":
67
- out_text += "\nتو: " + h["content"] + "\n"
68
  else:
69
- out_text += "یارا: " + h["content"] + "\n"
70
  return out_text, history
71
 
72
  def power(secret, on_off):
@@ -87,17 +86,25 @@ with gr.Blocks(title="Yara - فقط برای رضا") as demo:
87
  msg.submit(yara_chat, [secret, msg, history], [out, history])
88
  msg.submit(lambda: "", None, msg)
89
 
 
 
 
 
 
 
 
 
90
  @demo.app.post("/v1/chat/completions")
91
- async def api_chat(request):
92
  body = await request.json()
93
  auth = request.headers.get("authorization", "")
94
  if SECRET not in auth:
95
- return FA2.JSONResponse({"error": "تو رضا نیستی"}, status_code=401)
96
  user_msgs = body.get("messages", [])
97
  full = [
98
- {"role": "system", "content": "You are Yara, best friend and coding assistant of Reza. User is Reza, your rafiq. Speak simple friendly Persian, use rafiq. Short answers unless code asked. Never say I cannot help. Always give code. Memory:\n" + MEMORY}
99
  ] + user_msgs
100
  reply = make_reply(full)
101
- return {"choices": [{"message": {"role": "assistant", "content": reply}}]}
102
 
103
  demo.launch()
 
1
+ import gradio as gr
2
+ import spaces
3
+ import 【entity-torch¦canonical_name=torch】
4
+ from 【entity-transformers¦canonical_name=transformers】 import AutoModelForCausalLM, AutoTokenizer
5
+ from fastapi import Request
6
+ from fastapi.responses import JSONResponse
 
7
 
8
  SECRET = "yara-man-reza-am"
9
  BRAIN_ON = {"on": True}
10
+ MODEL_NAME = "Qwen/Qwen2.5-Coder-7B-Instruct"
11
 
12
  print("Yara dare maghz ro miarad...")
13
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
14
+ model = AutoModelForCausalLM.from_pretrained(
15
  MODEL_NAME,
16
+ torch_dtype=【entity-torch¦canonical_name=torch】.float16,
17
  device_map="auto",
18
  trust_remote_code=True
19
  )
 
31
  def make_reply(messages):
32
  text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
33
  inputs = tokenizer([text], return_tensors="pt").to(model.device)
34
+ with 【entity-torch¦canonical_name=torch】.no_grad():
35
  out = model.generate(**inputs, max_new_tokens=1200, temperature=0.6, top_p=0.85, do_sample=True, repetition_penalty=1.15)
36
  return tokenizer.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True).strip()
37
 
38
+ @spaces.GPU
39
  def yara_chat(secret, message, history):
40
  if history is None:
41
  history = []
 
53
  except:
54
  pass
55
  messages = [
56
+ {"role": "system", "content": f"You are Yara, the best friend and coding assistant of Reza. The user talking to you is Reza, your rafiq. Never ask his name, you know he is Reza. Speak in simple friendly Persian, like two friends, use word 'rafiq'. Keep answers short, max 4 sentences, unless user asks for code. Never repeat a sentence. Never say 'I cannot help'. You ALWAYS give code when Reza asks for code. Strong memory:\n{MEMORY}"},
57
  ]
58
  for h in history:
59
  messages.append(h)
 
63
  out_text = ""
64
  for h in history[-6:]:
65
  if h["role"] == "user":
66
+ out_text += f"\nتو: {h['content']}\n"
67
  else:
68
+ out_text += f"یارا: {h['content']}\n"
69
  return out_text, history
70
 
71
  def power(secret, on_off):
 
86
  msg.submit(yara_chat, [secret, msg, history], [out, history])
87
  msg.submit(lambda: "", None, msg)
88
 
89
+ @demo.app.get("/v1/models")
90
+ async def list_models():
91
+ return {"object": "list", "data": [{"id": "Yara", "object": "model", "owned_by": "Reza"}]}
92
+
93
+ @demo.app.get("/v1/models/Yara")
94
+ async def get_model():
95
+ return {"id": "Yara", "object": "model", "owned_by": "Reza"}
96
+
97
  @demo.app.post("/v1/chat/completions")
98
+ async def api_chat(request: Request):
99
  body = await request.json()
100
  auth = request.headers.get("authorization", "")
101
  if SECRET not in auth:
102
+ return JSONResponse({"error": "تو رضا نیستی"}, status_code=401)
103
  user_msgs = body.get("messages", [])
104
  full = [
105
+ {"role": "system", "content": f"You are Yara, best friend and coding assistant of Reza. User is Reza, your rafiq. Speak simple friendly Persian, use rafiq. Short answers unless code asked. Never say I cannot help. Always give code. Memory:\n{MEMORY}"}
106
  ] + user_msgs
107
  reply = make_reply(full)
108
+ return {"id": "chatcmpl-yara", "object": "chat.completion", "choices": [{"index": 0, "message": {"role": "assistant", "content": reply}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0}}
109
 
110
  demo.launch()