pmv-hou commited on
Commit
bd65e60
·
verified ·
1 Parent(s): 52addf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -32
app.py CHANGED
@@ -1,46 +1,24 @@
1
 
2
  import gradio as gr
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
- import torch
5
 
6
- model = AutoModelForCausalLM.from_pretrained(
7
- "tiiuae/falcon-7b-instruct",
8
- torch_dtype=torch.bfloat16,
9
- trust_remote_code=True,
10
- device_map="auto",
11
- low_cpu_mem_usage=True,
12
- )
13
- tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-7b-instruct")
14
 
15
 
16
- def generate_text(input_text):
17
- input_ids = tokenizer.encode(input_text, return_tensors="pt")
18
- attention_mask = torch.ones(input_ids.shape)
19
-
20
- output = model.generate(
21
- input_ids,
22
- attention_mask=attention_mask,
23
- max_length=200,
24
- do_sample=True,
25
- top_k=10,
26
- num_return_sequences=1,
27
- eos_token_id=tokenizer.eos_token_id,
28
- )
29
-
30
- output_text = tokenizer.decode(output[0], skip_special_tokens=True)
31
- print(output_text)
32
-
33
- # Remove Prompt Echo from Generated Text
34
- cleaned_output_text = output_text.replace(input_text, "")
35
- return cleaned_output_text
36
 
37
 
38
  text_generation_interface = gr.Interface(
39
- fn=generate_text,
40
  inputs=[
41
  gr.inputs.Textbox(label="Input Text"),
42
  ],
43
  outputs=gr.inputs.Textbox(label="Generated Text"),
44
- title="Falcon-7B Instruct",
45
 
46
  ).launch()
 
1
 
2
  import gradio as gr
3
+ import requests
 
4
 
5
+ API_TOKEN = os.environ['API_TOKEN']
 
 
 
 
 
 
 
6
 
7
 
8
+ API_URL = "https://api-inference.huggingface.co/google/gemma-7b"
9
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
10
+ def query(payload):
11
+ response = requests.post(API_URL, headers=headers, json=payload)
12
+ return response.json()
13
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
 
16
  text_generation_interface = gr.Interface(
17
+ fn=query,
18
  inputs=[
19
  gr.inputs.Textbox(label="Input Text"),
20
  ],
21
  outputs=gr.inputs.Textbox(label="Generated Text"),
22
+ title="google/gemma-7b",
23
 
24
  ).launch()