rodrigomasini commited on
Commit
ea970bb
·
verified ·
1 Parent(s): d19b3b8

Update app_backup.py

Browse files
Files changed (1) hide show
  1. app_backup.py +117 -68
app_backup.py CHANGED
@@ -1,81 +1,130 @@
1
- import gradio as gr
 
 
 
2
  import os
3
- import spaces
4
- from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
5
- from threading import Thread
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- # Set an environment variable
8
- HF_TOKEN = os.environ.get("HF_TOKEN", None)
 
 
 
 
 
 
 
 
 
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- DESCRIPTION = '''
12
- <div>
13
- <h1 style="text-align: center;">Mistral 7B Instruct v0.3</h1>
14
- </div>
15
- '''
 
 
 
16
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Load the tokenizer and model
19
- tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
20
- model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3", device_map="auto")
21
- terminators = [
22
- tokenizer.eos_token_id,
23
- tokenizer.convert_tokens_to_ids("<|eot_id|>")
24
- ]
 
 
25
 
26
- @spaces.GPU(duration=120)
27
- def chat_mistral7b_v0dot3(message: str,
28
- history: list,
29
- temperature: float,
30
- max_new_tokens: int
31
- ) -> str:
32
- """
33
- Generate a streaming response using the mistralai/Mistral-7B-Instruct-v0.3 model.
34
- Args:
35
- message (str): The input message.
36
- history (list): The conversation history used by ChatInterface.
37
- temperature (float): The temperature for generating the response.
38
- max_new_tokens (int): The maximum number of new tokens to generate.
39
- Returns:
40
- str: The generated response.
41
- """
42
- conversation = []
43
- for user, assistant in history:
44
- conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
45
- conversation.append({"role": "user", "content": message})
46
 
47
- input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
 
 
 
 
 
48
 
49
- streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
50
 
51
- generate_kwargs = dict(
52
- input_ids= input_ids,
53
- streamer=streamer,
54
- max_new_tokens=max_new_tokens,
55
- do_sample=True,
56
- temperature=temperature,
57
- eos_token_id=terminators,
58
- )
59
- # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
60
- if temperature == 0:
61
- generate_kwargs['do_sample'] = False
62
-
63
- t = Thread(target=model.generate, kwargs=generate_kwargs)
64
- t.start()
65
 
66
- outputs = []
67
- for text in streamer:
68
- outputs.append(text)
69
- yield "".join(outputs)
 
 
 
 
70
 
71
  with gr.Blocks() as demo:
72
-
73
- gr.Interface(
74
- fn=chat_mistral7b_v0dot3,
75
- inputs=[gr.Textbox(), gr.Textbox(), gr.Number(), gr.Number()],
76
- outputs=[gr.Textbox()]
77
- )
78
-
79
-
80
- if __name__ == "__main__":
81
- demo.launch()
 
1
+ # Contact us:
2
+ import platform
3
+ import sys
4
+ import shutil
5
  import os
6
+ import datetime
7
+ import subprocess
8
+ import gradio as gr
9
+ import pathlib
10
+
11
+ def check_python():
12
+ supported_minors = [9, 10, 11]
13
+ python_info = f"Python {platform.python_version()} on {platform.system()}"
14
+ if not (
15
+ int(sys.version_info.major) == 3
16
+ and int(sys.version_info.minor) in supported_minors
17
+ ):
18
+ python_info += f"\nIncompatible Python version: {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} required 3.{supported_minors}"
19
+ return python_info
20
+
21
+ def check_torch():
22
+ torch_info = ""
23
+ if shutil.which('nvidia-smi') is not None or os.path.exists(
24
+ os.path.join(
25
+ os.environ.get('SystemRoot') or r'C:\Windows',
26
+ 'System32',
27
+ 'nvidia-smi.exe',
28
+ )
29
+ ):
30
+ torch_info += 'NVIDIA toolkit detected\n'
31
+ elif shutil.which('rocminfo') is not None or os.path.exists('/opt/rocm/bin/rocminfo'):
32
+ torch_info += 'AMD toolkit detected\n'
33
+ elif (shutil.which('sycl-ls') is not None
34
+ or os.environ.get('ONEAPI_ROOT') is not None
35
+ or os.path.exists('/opt/intel/oneapi')):
36
+ torch_info += 'Intel OneAPI toolkit detected\n'
37
+ else:
38
+ torch_info += 'Using CPU-only Torch\n'
39
 
40
+ try:
41
+ import torch
42
+ try:
43
+ import intel_extension_for_pytorch as ipex
44
+ if torch.xpu.is_available():
45
+ from library.ipex import ipex_init
46
+ ipex_init()
47
+ os.environ.setdefault('NEOReadDebugKeys', '1')
48
+ os.environ.setdefault('ClDeviceGlobalMemSizeAvailablePercent', '100')
49
+ except Exception:
50
+ pass
51
+ torch_info += f'Torch {torch.__version__}\n'
52
 
53
+ if not torch.cuda.is_available():
54
+ torch_info += 'Torch reports CUDA not available\n'
55
+ else:
56
+ if torch.version.cuda:
57
+ if hasattr(torch, "xpu") and torch.xpu.is_available():
58
+ torch_info += f'Torch backend: Intel IPEX OneAPI {ipex.__version__}\n'
59
+ else:
60
+ torch_info += f'Torch backend: NVIDIA CUDA {torch.version.cuda} cuDNN {torch.backends.cudnn.version() if torch.backends.cudnn.is_available() else "N/A"}\n'
61
+ elif torch.version.hip:
62
+ torch_info += f'Torch backend: AMD ROCm HIP {torch.version.hip}\n'
63
+ else:
64
+ torch_info += 'Unknown Torch backend\n'
65
 
66
+ for device in [torch.cuda.device(i) for i in range(torch.cuda.device_count())]:
67
+ if hasattr(torch, "xpu") and torch.xpu.is_available():
68
+ torch_info += f'Torch detected GPU: {torch.xpu.get_device_name(device)} VRAM {round(torch.xpu.get_device_properties(device).total_memory / 1024 / 1024)} Compute Units {torch.xpu.get_device_properties(device).max_compute_units}\n'
69
+ else:
70
+ torch_info += f'Torch detected GPU: {torch.cuda.get_device_name(device)} VRAM {round(torch.cuda.get_device_properties(device).total_memory / 1024 / 1024)} Arch {torch.cuda.get_device_capability(device)} Cores {torch.cuda.get_device_properties(device).multi_processor_count}\n'
71
+ return torch_info
72
+ except Exception as e:
73
+ return f'Could not load torch: {e}'
74
 
75
+ def get_installed_packages():
76
+ pkgs_installed = subprocess.getoutput("pip freeze")
77
+ output_lines = [line for line in pkgs_installed.splitlines() if "WARNING" not in line]
78
+ packages = []
79
+ for line in output_lines:
80
+ if '==' in line:
81
+ pkg_name, pkg_version = line.split('==')
82
+ packages.append((pkg_name, pkg_version))
83
+ packages.sort(key=lambda x: x[0].lower())
84
+ return packages
85
 
86
+ def get_installed_packages_version():
87
+ pkgs_installed = subprocess.getoutput("pip freeze")
88
+ output_lines = [line for line in pkgs_installed.splitlines() if "WARNING" not in line]
89
+ packages_version = []
90
+ for line in output_lines:
91
+ if '==' in line:
92
+ pkg_name, pkg_version = line.split('==')
93
+ packages_version.append((pkg_name, pkg_version))
94
+ return packages_version
95
 
96
+ def match_packages_with_versions():
97
+ #local = pathlib.Path(__file__).parent
98
+ requirements_file = os.path.join(os.path.dirname(__file__),"requirements.txt")
99
+ with open(requirements_file, 'r') as f:
100
+ requirements = f.read().splitlines()
101
+
102
+ requirements = [req.split('==')[0] for req in requirements if req and not req.startswith('#')]
103
+ installed_packages = get_installed_packages_version()
104
+ installed_dict = {pkg: version for pkg, version in installed_packages}
 
 
 
 
 
 
 
 
 
 
 
105
 
106
+ matched_packages = []
107
+ for req in requirements:
108
+ if req in installed_dict:
109
+ matched_packages.append((req, installed_dict[req]))
110
+ else:
111
+ matched_packages.append((req, "Not installed"))
112
 
113
+ return matched_packages
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
+ def display_info():
117
+ current_datetime = datetime.datetime.now()
118
+ current_datetime_str = current_datetime.strftime('%m/%d/%Y-%H:%M:%S')
119
+ python_info = check_python()
120
+ torch_info = check_torch()
121
+ packages = get_installed_packages()
122
+ versions = match_packages_with_versions()
123
+ return f"Machine local date and time: {current_datetime_str}\n\n{python_info}\n\n{torch_info}\n\nInstalled packages:\n{packages}\n\nMatched packages with versions:\n{versions}"
124
 
125
  with gr.Blocks() as demo:
126
+ output = gr.Textbox(lines=20, label="System Information")
127
+ btn = gr.Button("Check System")
128
+ btn.click(display_info, inputs=[], outputs=[output])
129
+
130
+ demo.launch()