khulnasoft commited on
Commit
a5c65d1
·
verified ·
1 Parent(s): b4dfe59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py CHANGED
@@ -1,3 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Read the contents of setup.py
2
  with open("setup.py", "r") as file:
3
  setup_content = file.read()
 
1
+ import os
2
+
3
+ from threading import Thread
4
+ from typing import Iterator
5
+
6
+ import gradio as gr
7
+ import spaces
8
+ import torch
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
10
+
11
+ MAX_MAX_NEW_TOKENS = 2048
12
+ DEFAULT_MAX_NEW_TOKENS = 1024
13
+ total_count=0
14
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
15
+
16
+ if not torch.cuda.is_available():
17
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
18
+
19
+
20
+ if torch.cuda.is_available():
21
+ model_id = "khulnasoft/gpt-computer-agent"
22
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
23
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
24
+ tokenizer.use_default_system_prompt = False
25
+
26
+
27
+ @spaces.GPU
28
+ def generate(
29
+ message: str,
30
+ chat_history: list[tuple[str, str]],
31
+ system_prompt: str,
32
+ max_new_tokens: int = 1024,
33
+ temperature: float = 0.6,
34
+ top_p: float = 0.9,
35
+ top_k: int = 50,
36
+ repetition_penalty: float = 1,
37
+ ) -> Iterator[str]:
38
+ global total_count
39
+ total_count += 1
40
+ print(total_count)
41
+ os.system("nvidia-smi")
42
+ conversation = []
43
+ if system_prompt:
44
+ conversation.append({"role": "system", "content": system_prompt})
45
+ for user, assistant in chat_history:
46
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
47
+ conversation.append({"role": "user", "content": message})
48
+
49
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
50
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
51
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
52
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
53
+ input_ids = input_ids.to(model.device)
54
+
55
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
56
+ generate_kwargs = dict(
57
+ {"input_ids": input_ids},
58
+ streamer=streamer,
59
+ max_new_tokens=max_new_tokens,
60
+ do_sample=False,
61
+ top_p=top_p,
62
+ top_k=top_k,
63
+ num_beams=1,
64
+ # temperature=temperature,
65
+ repetition_penalty=repetition_penalty,
66
+ eos_token_id=32021
67
+ )
68
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
69
+ t.start()
70
+
71
+ outputs = []
72
+ for text in streamer:
73
+ outputs.append(text)
74
+ yield "".join(outputs).replace("<|EOT|>","")
75
+
76
+
77
  # Read the contents of setup.py
78
  with open("setup.py", "r") as file:
79
  setup_content = file.read()