X34o commited on
Commit
d7715fb
·
verified ·
1 Parent(s): 5b134af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -1,29 +1,48 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- import os
4
 
5
- token = os.environ.get("HF_TOKEN")
6
 
7
- client = InferenceClient(
8
- model="HuggingFaceH4/zephyr-7b-beta",
9
- token=token
 
 
10
  )
 
11
 
12
  def generate(prompt):
13
- try:
14
- response = client.chat_completion(
15
- messages=[{"role": "user", "content": prompt}],
16
- max_tokens=512,
17
- temperature=0.7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  )
19
- return response.choices[0].message.content
20
- except Exception as e:
21
- return f"Error: {str(e)}"
 
22
 
23
  gr.Interface(
24
  fn=generate,
25
  inputs=gr.Textbox(lines=4, label="Tanya apa saja"),
26
  outputs=gr.Textbox(label="Jawaban"),
27
  title="AI Assistant",
28
- description="Powered by HF Inference API"
29
  ).launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
 
5
+ model_name = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
6
 
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ dtype=torch.float32,
11
+ low_cpu_mem_usage=True
12
  )
13
+ model.eval()
14
 
15
  def generate(prompt):
16
+ formatted = f"### Instruction:\n{prompt}\n### Response:\n"
17
+
18
+ inputs = tokenizer(
19
+ formatted,
20
+ return_tensors="pt",
21
+ truncation=True,
22
+ max_length=512
23
+ )
24
+
25
+ input_ids = inputs["input_ids"]
26
+ attention_mask = inputs["attention_mask"]
27
+
28
+ with torch.no_grad():
29
+ output = model.generate(
30
+ input_ids=input_ids,
31
+ attention_mask=attention_mask,
32
+ max_new_tokens=256,
33
+ do_sample=True,
34
+ temperature=0.7,
35
+ pad_token_id=tokenizer.eos_token_id
36
  )
37
+
38
+ new_tokens = output[0][input_ids.shape[-1]:]
39
+ result = tokenizer.decode(new_tokens, skip_special_tokens=True)
40
+ return result.strip()
41
 
42
  gr.Interface(
43
  fn=generate,
44
  inputs=gr.Textbox(lines=4, label="Tanya apa saja"),
45
  outputs=gr.Textbox(label="Jawaban"),
46
  title="AI Assistant",
47
+ description="Powered by Qwen2.5-Coder 1.5B"
48
  ).launch()