dingckc commited on
Commit
290f301
·
verified ·
1 Parent(s): 219e0b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -1,11 +1,13 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
  import os
 
 
 
4
 
5
- # 初始化 Inference API 客戶端
6
  model_id = "dingckc/FineLlama-3.1-8B"
7
- api_token = os.getenv('ACCESS_KEY')
8
- inference = InferenceClient(model=model_id, token=api_token)
 
9
 
10
  # 定義推理函數
11
  def evaluate_essay(title, essay):
@@ -15,9 +17,10 @@ def evaluate_essay(title, essay):
15
  Essay: {essay}
16
  Please generate a detailed evaluation based on the rubric provided above.
17
  """
18
- # 使用 text_generation 方法進行推理
19
- response = inference.text_generation(input_text)
20
- return response[0]["generated_text"] if "generated_text" in response[0] else "No evaluation available."
 
21
 
22
  # 使用 Gradio 構建界面
23
  title_input = gr.Textbox(label="Essay Title")
@@ -30,4 +33,4 @@ gr.Interface(
30
  outputs=output_text,
31
  title="Essay Evaluation",
32
  description="Enter the title and content of your essay to receive an evaluation."
33
- ).launch()
 
 
 
1
  import os
2
+ import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import torch
5
 
6
+ # 設置模型 ID 和加載 Hugging Face API token
7
  model_id = "dingckc/FineLlama-3.1-8B"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=os.getenv('ACCESS_KEY'))
9
+ model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=os.getenv('ACCESS_KEY'))
10
+ model = model.to("cuda" if torch.cuda.is_available() else "cpu")
11
 
12
  # 定義推理函數
13
  def evaluate_essay(title, essay):
 
17
  Essay: {essay}
18
  Please generate a detailed evaluation based on the rubric provided above.
19
  """
20
+ inputs = tokenizer(input_text, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
21
+ with torch.no_grad():
22
+ outputs = model.generate(input_ids=inputs["input_ids"], max_new_tokens=150)
23
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
24
 
25
  # 使用 Gradio 構建界面
26
  title_input = gr.Textbox(label="Essay Title")
 
33
  outputs=output_text,
34
  title="Essay Evaluation",
35
  description="Enter the title and content of your essay to receive an evaluation."
36
+ ).launch(share=True)