Nitzantry1 commited on
Commit
eff6cb5
verified
1 Parent(s): 8106ffa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -22
app.py CHANGED
@@ -1,34 +1,25 @@
1
- import torch
2
- from transformers import pipeline
3
  import os
4
  import gradio as gr
 
5
 
6
  model_id = 'dicta-il/dictalm-7b-instruct'
7
 
8
- # 讟注讬谞转 讛诪讜讚诇 讜讛讻谞转 讛诪谞讜注
9
- should_use_fast = True
10
- print(f'should_use_fast = {should_use_fast}')
11
-
12
- local_rank = int(os.getenv('LOCAL_RANK', '0'))
13
- world_size = int(os.getenv('WORLD_SIZE', '1'))
14
-
15
- # 讘讚讬拽转 讛转拽谉 - GPU 讗讜 CPU
16
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
17
- print('Using device:', device)
18
- print()
19
-
20
- # 讬爪讬专转 讛诪谞讜注 注诐 pipeline
21
- generator = pipeline('text-generation', model=model_id,
22
- tokenizer=model_id,
23
- torch_dtype=torch.float16 if device.type == 'cuda' else torch.float32,
24
- use_fast=should_use_fast,
25
- trust_remote_code=True,
26
- device_map="auto" if device.type == 'cuda' else None)
27
 
28
  # 驻讜谞拽爪讬讬转 讬爪讬专转 讛讟拽住讟
29
  def chat_with_model(history):
30
  prompt = history[-1]["content"]
31
- result = generator(prompt, do_sample=True, min_length=20, max_length=64, top_k=40, top_p=0.92, temperature=0.9)[0]["generated_text"]
 
 
 
 
 
 
 
 
32
  return history + [{"role": "bot", "content": result}]
33
 
34
  # 讬爪讬专转 诪诪砖拽 诪转拽讚诐 注诐 Gradio 讘爪讜专转 爪'讟-讘讜讟 讘住讙谞讜谉 讗拽讚诪讬
 
 
 
1
  import os
2
  import gradio as gr
3
+ from huggingface_hub import InferenceApi
4
 
5
  model_id = 'dicta-il/dictalm-7b-instruct'
6
 
7
+ # 讬爪讬专转 API 砖诇 Hugging Face
8
+ api_key = os.getenv('HUGGINGFACE_API_KEY', '')
9
+ generator = InferenceApi(repo_id=model_id, token=api_key)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # 驻讜谞拽爪讬讬转 讬爪讬专转 讛讟拽住讟
12
  def chat_with_model(history):
13
  prompt = history[-1]["content"]
14
+ response = generator(prompt, parameters={
15
+ "do_sample": True,
16
+ "min_length": 20,
17
+ "max_length": 64,
18
+ "top_k": 40,
19
+ "top_p": 0.92,
20
+ "temperature": 0.9
21
+ })
22
+ result = response["generated_text"]
23
  return history + [{"role": "bot", "content": result}]
24
 
25
  # 讬爪讬专转 诪诪砖拽 诪转拽讚诐 注诐 Gradio 讘爪讜专转 爪'讟-讘讜讟 讘住讙谞讜谉 讗拽讚诪讬