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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -51
app.py CHANGED
@@ -1,80 +1,45 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
- from huggingface_hub import login
4
  import torch
5
- import os
6
 
7
- # ๐Ÿ”น ุงู„ุญุตูˆู„ ุนู„ู‰ ุงู„ุชูˆูƒู† ู…ู† Secrets
8
- hf_token = os.getenv("HF_TOKEN")
9
-
10
- # ๐Ÿ”น ุชุณุฌูŠู„ ุงู„ุฏุฎูˆู„ ุฅู„ู‰ Hugging Face
11
- if hf_token:
12
- login(token=hf_token)
13
- else:
14
- raise ValueError("Hugging Face token is missing. Please check your secrets.")
15
-
16
- # ๐Ÿ”น ุงุณุชุฎุฏุงู… ู†ู…ูˆุฐุฌ TinyLlama ุงู„ุฃุฎู
17
- model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
18
-
19
- # ๐Ÿ”น ุชุญุฏูŠุฏ ุงู„ุฌู‡ุงุฒ ุงู„ู…ู†ุงุณุจ (GPU ุฃูˆ CPU)
20
  device = "cuda" if torch.cuda.is_available() else "cpu"
21
 
22
- # ๐Ÿ”น ุชุญู…ูŠู„ ุงู„ู†ู…ูˆุฐุฌ ูˆุงู„ู…ุญูˆู„ ู…ุน ุชุญุณูŠู† ุงู„ุฃุฏุงุก
 
23
  model = AutoModelForCausalLM.from_pretrained(
24
  model_name,
25
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
26
- device_map="cuda" if torch.cuda.is_available() else "cpu",
27
  trust_remote_code=True
28
- ).eval() # ๐Ÿ”ฅ ุชุญุณูŠู† ุงู„ุณุฑุนุฉ ุจุชุนุทูŠู„ ุงู„ุชุฏุฑูŠุจ
29
 
30
  tokenizer = AutoTokenizer.from_pretrained(model_name)
31
-
32
- # ๐Ÿ”น ุญู„ ู…ุดูƒู„ุฉ ุงู„ู€ padding
33
  tokenizer.pad_token = tokenizer.eos_token
34
 
35
- # ๐Ÿ”น ุฑุฏูˆุฏ ู…ุฎุตุตุฉ (Instant Responses)
36
- custom_responses = {
37
- "ู…ุง ู‡ูˆ ู‡ุฏููƒุŸ": "ู‡ุฏููŠ ู‡ูˆ ุชู‚ุฏูŠู… ุงู„ู…ุณุงุนุฏุฉ ูˆุงู„ุฅุฌุงุจุฉ ุนู„ู‰ ุฃุณุฆู„ุชูƒ ุจุฃูุถู„ ุทุฑูŠู‚ุฉ ู…ู…ูƒู†ุฉ.",
38
- "who created you?": "I was created by George.",
39
- "who programmed you?": "My programmer is George.",
40
- "what is your name": "I am Octagon 2.0.",
41
- "do you have an owner?": "I am owned by George.",
42
- "what is your purpose": "My purpose is to assist and answer questions in the best way.",
43
- "ไป‹็ปไฝ ่‡ชๅทฑ": "ๆˆ‘ๆ˜ฏ Octagon 2.0ใ€‚",
44
- "ไฝ ๆ˜ฏ่ฐๅˆ›้€ ็š„๏ผŸ": "ๆˆ‘ๆ˜ฏ็”ฑ George ๅˆ›้€ ็š„ใ€‚",
45
- "ไฝ ็š„็ผ–็จ‹่€…ๆ˜ฏ่ฐ๏ผŸ": "ๆˆ‘็š„็ผ–็จ‹่€…ๆ˜ฏ Georgeใ€‚",
46
- }
47
-
48
- # ๐Ÿ”น ุฏุงู„ุฉ ุงู„ุฏุฑุฏุดุฉ ุงู„ุณุฑูŠุนุฉ
49
  def chatbot(user_input):
50
  if not user_input.strip():
51
  return "Please enter a message."
52
 
53
- user_input = user_input.lower()
54
-
55
- # ุงู„ุชุญู‚ู‚ ู…ู† ุงู„ุฑุฏูˆุฏ ุงู„ู…ุฎุตุตุฉ ุฃูˆู„ุงู‹
56
- for question, answer in custom_responses.items():
57
- if question in user_input:
58
- return answer
59
-
60
- # ๐Ÿ”ฅ ุชุญุณูŠู† ุงู„ุฃุฏุงุก ุจุงุณุชุฎุฏุงู… ุงู„ู…ุนู„ู…ุงุช ุงู„ุชุงู„ูŠุฉ:
61
- inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True).to(device)
62
 
63
- with torch.no_grad(): # ุชุญุณูŠู† ุงู„ุณุฑุนุฉ
64
  output = model.generate(
65
  **inputs,
66
- max_length=50, # ๐Ÿ”น ุชู‚ู„ูŠู„ ุงู„ุทูˆู„ ุงู„ุฃู‚ุตู‰ ู„ุชุณุฑูŠุน ุงู„ุงุณุชุฌุงุจุฉ
67
- temperature=0.7, # ๐Ÿ”น ุถุจุท ุงู„ุญุฑุงุฑุฉ ู„ุฌุนู„ ุงู„ุฑุฏูˆุฏ ุฃุณุฑุน ูˆุฃูุถู„
68
- top_p=0.9, # ๐Ÿ”น ุชู‚ู„ูŠู„ ุงู„ุงุญุชู…ุงู„ุงุช ุบูŠุฑ ุงู„ู…ููŠุฏุฉ
69
- do_sample=True, # ๐Ÿ”น ุงู„ุณู…ุงุญ ุจุงู„ุชูˆู„ูŠุฏ ุงู„ุนุดูˆุงุฆูŠ ู„ุชุฌู†ุจ ุงู„ุชูƒุฑุงุฑ
70
- early_stopping=True, # ๐Ÿ”น ุฅู†ู‡ุงุก ุงู„ุชูˆู„ูŠุฏ ุนู†ุฏู…ุง ูŠูƒูˆู† ูƒุงููŠู‹ุง
 
71
  pad_token_id=tokenizer.eos_token_id
72
  )
73
 
74
  response = tokenizer.decode(output[0], skip_special_tokens=True)
75
-
76
  return response
77
 
78
- # ๐Ÿ”น ุชุดุบูŠู„ ุงู„ูˆุงุฌู‡ุฉ ู…ุน Gradio
79
  iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Octagon 2.0 Chatbot")
80
  iface.launch(share=True)
 
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)