Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,40 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
|
|
|
42 |
|
|
|
|
|
43 |
"""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Hugging Face.ipynb
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
Automatically generated by Colab.
|
5 |
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1zRuAxGm_11lNIeBxFlHVzc5tNKhyLef4
|
8 |
"""
|
9 |
+
import gradio as gr
|
10 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
11 |
+
|
12 |
+
# 加載 LLaMA 模型
|
13 |
+
model_name = "meta-llama/Llama-2-7b-hf"
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
16 |
+
|
17 |
+
# 定義推理函數
|
18 |
+
def generate_text(prompt):
|
19 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
20 |
+
outputs = model.generate(
|
21 |
+
inputs.input_ids,
|
22 |
+
max_length=200,
|
23 |
+
num_beams=5,
|
24 |
+
repetition_penalty=1.2,
|
25 |
+
early_stopping=True
|
26 |
+
)
|
27 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
28 |
+
|
29 |
+
# 使用 Gradio 构建界面
|
30 |
+
interface = gr.Interface(
|
31 |
+
fn=generate_text,
|
32 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter your prompt here..."),
|
33 |
+
outputs="text",
|
34 |
+
title="LLaMA Text Generator",
|
35 |
+
description="Generate text using LLaMA 2 models hosted on Hugging Face Spaces."
|
36 |
)
|
37 |
|
38 |
+
# 啟動應用
|
39 |
if __name__ == "__main__":
|
40 |
+
interface.launch()
|