Georg4000 commited on
Commit
f2ceb0a
ยท
verified ยท
1 Parent(s): 29a7c13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -1,45 +1,47 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
- import os
5
 
6
  # ๐Ÿ”ฅ ุชุญุฏูŠุฏ ุงู„ุฌู‡ุงุฒ ุงู„ู…ู†ุงุณุจ
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
- # ๐Ÿ”ฅ ุชุญู…ูŠู„ ุงู„ู…ูˆุฏูŠู„ ุจุทุฑูŠู‚ุฉ ุฃุณุฑุน
10
  model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
11
  model = AutoModelForCausalLM.from_pretrained(
12
  model_name,
13
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
14
- device_map="auto",
15
  trust_remote_code=True
16
  ).eval()
17
 
18
- tokenizer = AutoTokenizer.from_pretrained(model_name)
 
19
  tokenizer.pad_token = tokenizer.eos_token
20
 
21
- # ๐Ÿ”ฅ ุฅุนุฏุงุฏุงุช ุฃุณุฑุน ู„ู„ุชูˆู„ูŠุฏ
22
  def chatbot(user_input):
23
  if not user_input.strip():
24
  return "Please enter a message."
25
 
26
- inputs = tokenizer(user_input, return_tensors="pt").to(device)
27
 
28
  with torch.no_grad():
29
  output = model.generate(
30
- **inputs,
31
- max_length=50, # ๐ŸŸข ุชู‚ู„ูŠู„ ุงู„ุทูˆู„ ู„ุชุณุฑูŠุน ุงู„ุชูˆู„ูŠุฏ
32
- temperature=0.6, # ๐ŸŸข ุชู‚ู„ูŠู„ ุงู„ุนุดูˆุงุฆูŠุฉ
33
- top_p=0.8, # ๐ŸŸข ุงุฎุชูŠุงุฑ ุงู„ูƒู„ู…ุงุช ุงู„ุฃูƒุซุฑ ุงุญุชู…ุงู„ูŠุฉ
 
34
  do_sample=True,
35
  early_stopping=True,
36
- num_return_sequences=1,
37
  pad_token_id=tokenizer.eos_token_id
38
  )
39
 
40
  response = tokenizer.decode(output[0], skip_special_tokens=True)
41
  return response
42
 
43
- # ๐Ÿ”ฅ ุชุดุบูŠู„ ุงู„ูˆุงุฌู‡ุฉ
44
  iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Octagon 2.0 Chatbot")
45
- iface.launch(share=True)
 
 
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
 
4
 
5
  # ๐Ÿ”ฅ ุชุญุฏูŠุฏ ุงู„ุฌู‡ุงุฒ ุงู„ู…ู†ุงุณุจ
6
  device = "cuda" if torch.cuda.is_available() else "cpu"
7
 
8
+ # ๐Ÿ”ฅ ุชุญู…ูŠู„ ุงู„ู…ูˆุฏูŠู„ ุจุทุฑูŠู‚ุฉ ู…ุญุณู†ุฉ
9
  model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
10
  model = AutoModelForCausalLM.from_pretrained(
11
  model_name,
12
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
13
+ device_map="auto" if torch.cuda.is_available() else None, # โœ… ุถุจุท ุงู„ู€ device_map ุจุดูƒู„ ุฃุฏู‚
14
  trust_remote_code=True
15
  ).eval()
16
 
17
+ # ๐Ÿ”ฅ ุชุญู…ูŠู„ ุงู„ุชูˆูƒู†ูŠุฒุฑ
18
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
19
  tokenizer.pad_token = tokenizer.eos_token
20
 
21
+ # ๐Ÿ”ฅ ุฅุนุฏุงุฏ ุงู„ุฏุงู„ุฉ ุงู„ุฎุงุตุฉ ุจุงู„ู€ Chatbot
22
  def chatbot(user_input):
23
  if not user_input.strip():
24
  return "Please enter a message."
25
 
26
+ inputs = tokenizer(user_input, return_tensors="pt", padding=True).to(device)
27
 
28
  with torch.no_grad():
29
  output = model.generate(
30
+ input_ids=inputs["input_ids"],
31
+ attention_mask=inputs["attention_mask"], # โœ… ุฅุตู„ุงุญ ู…ุดูƒู„ุฉ ุงู„ู…ุงุณูƒ
32
+ max_new_tokens=50, # โœ… ุงุณุชุฎุฏุงู… max_new_tokens ุจุฏู„ุงู‹ ู…ู† max_length
33
+ temperature=0.6,
34
+ top_p=0.8,
35
  do_sample=True,
36
  early_stopping=True,
 
37
  pad_token_id=tokenizer.eos_token_id
38
  )
39
 
40
  response = tokenizer.decode(output[0], skip_special_tokens=True)
41
  return response
42
 
43
+ # ๐Ÿ”ฅ ุชุดุบูŠู„ ุงู„ูˆุงุฌู‡ุฉ ุจุฏูˆู† share=True
44
  iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Octagon 2.0 Chatbot")
45
+
46
+ # โœ… ุฅุตู„ุงุญ ู…ุดูƒู„ุฉ POST method not allowed
47
+ iface.launch(ssl=False, debug=True)