Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -41,12 +41,12 @@ if token:
|
|
41 |
login(token)
|
42 |
print("讛转讞讘专讜转 诇-Hugging Face 讛爪诇讬讞讛!")
|
43 |
else:
|
44 |
-
print("讗讝讛专讛: 讟讜拽谉 HF_TOKEN 诇讗 诪讜讙讚专 讘住讘讬讘转 讛注讘讜讚讛. 讬讬转讻谉 砖诇讗 转讛讬讛 讙讬砖讛
|
45 |
|
46 |
-
# 讛讙讚专转 砖诐 讛诪讜讚诇
|
47 |
-
model_name = "
|
48 |
|
49 |
-
#
|
50 |
print(f"讟讜注谉 诪讜讚诇 {model_name}...")
|
51 |
generator = None
|
52 |
try:
|
@@ -55,29 +55,67 @@ try:
|
|
55 |
model=model_name,
|
56 |
device_map="auto",
|
57 |
torch_dtype="auto",
|
|
|
58 |
model_kwargs={"quantization_config": {"load_in_4bit": True}}
|
59 |
)
|
60 |
print("讟注讬谞转 讛诪讜讚诇 讛爪诇讬讞讛!")
|
61 |
except Exception as e:
|
62 |
print(f"砖讙讬讗讛 讘讟注讬谞转 讛诪讜讚诇: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
def ask_model(prompt):
|
65 |
if generator is None:
|
66 |
return "讛诪讜讚诇 诇讗 谞讟注谉 讘讛爪诇讞讛. 讘讚讜拽 讗转 诇讜讙 讛砖讙讬讗讜转."
|
67 |
|
68 |
try:
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
except Exception as e:
|
76 |
return f"砖讙讬讗讛 讘讛驻注诇转 讛诪讜讚诇: {str(e)}"
|
77 |
|
78 |
# 讬爪讬专转 诪诪砖拽 诪砖转诪砖
|
79 |
with gr.Blocks() as demo:
|
80 |
-
gr.Markdown("#
|
81 |
gr.Markdown("讻转讜讘 砖讗诇讛 讗讜 讘拽砖讛 讜讛诪讜讚诇 讬注谞讛:")
|
82 |
|
83 |
with gr.Row():
|
@@ -95,10 +133,18 @@ with gr.Blocks() as demo:
|
|
95 |
[
|
96 |
"诪讛 讚注转讱 注诇 讘讬谞讛 诪诇讗讻讜转讬转?",
|
97 |
"讻转讜讘 住讬驻讜专 拽爪专 注诇 讞转讜诇 讜讻诇讘 砖讛诐 讞讘专讬诐",
|
98 |
-
"讛住讘专 诇讬 讗转 转讜专转 讛讬讞住讜转 讘爪讜专讛 驻砖讜讟讛"
|
|
|
99 |
],
|
100 |
input_text
|
101 |
)
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
# 讛驻注诇转 讛诪诪砖拽
|
104 |
demo.launch()
|
|
|
41 |
login(token)
|
42 |
print("讛转讞讘专讜转 诇-Hugging Face 讛爪诇讬讞讛!")
|
43 |
else:
|
44 |
+
print("讗讝讛专讛: 讟讜拽谉 HF_TOKEN 诇讗 诪讜讙讚专 讘住讘讬讘转 讛注讘讜讚讛. 讬讬转讻谉 砖诇讗 转讛讬讛 讙讬砖讛 诇诪讜讚诇讬诐 诪讜讙讘诇讬诐.")
|
45 |
|
46 |
+
# 讛讙讚专转 砖诐 讛诪讜讚诇 - 诪砖转诪砖 讘诪讜讚诇 驻转讜讞 砖诇讗 讚讜专砖 讗讬砖讜专 诪讬讜讞讚
|
47 |
+
model_name = "Fedifehmi/gemma3n-e2b-children-storyteller" # 诪讜讚诇 诪讘讜住住 Gemma 驻转讜讞
|
48 |
|
49 |
+
# 谞讬住讬讜谉 讟注讬谞讛 砖诇 讛诪讜讚诇
|
50 |
print(f"讟讜注谉 诪讜讚诇 {model_name}...")
|
51 |
generator = None
|
52 |
try:
|
|
|
55 |
model=model_name,
|
56 |
device_map="auto",
|
57 |
torch_dtype="auto",
|
58 |
+
trust_remote_code=True, # 诇讛讜住讬祝 转诪讬讻讛 讘拽讜讚 诪专讜讞拽
|
59 |
model_kwargs={"quantization_config": {"load_in_4bit": True}}
|
60 |
)
|
61 |
print("讟注讬谞转 讛诪讜讚诇 讛爪诇讬讞讛!")
|
62 |
except Exception as e:
|
63 |
print(f"砖讙讬讗讛 讘讟注讬谞转 讛诪讜讚诇: {str(e)}")
|
64 |
+
# 谞讬住讬讜谉 诇讟注讜谉 诪讜讚诇 驻转讜讞 驻砖讜讟 讬讜转专 讘诪拽专讛 砖诇 讻讬砖诇讜谉
|
65 |
+
try:
|
66 |
+
print("诪谞住讛 诇讟注讜谉 诪讜讚诇 讞诇讜驻讬...")
|
67 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
68 |
+
generator = transformers.pipeline(
|
69 |
+
"text-generation",
|
70 |
+
model=model_name,
|
71 |
+
device_map="auto",
|
72 |
+
torch_dtype="auto",
|
73 |
+
trust_remote_code=True,
|
74 |
+
model_kwargs={"quantization_config": {"load_in_4bit": True}}
|
75 |
+
)
|
76 |
+
print("讟注讬谞转 讛诪讜讚诇 讛讞诇讜驻讬 讛爪诇讬讞讛!")
|
77 |
+
except Exception as e2:
|
78 |
+
print(f"砖讙讬讗讛 讘讟注讬谞转 讛诪讜讚诇 讛讞诇讜驻讬: {str(e2)}")
|
79 |
|
80 |
def ask_model(prompt):
|
81 |
if generator is None:
|
82 |
return "讛诪讜讚诇 诇讗 谞讟注谉 讘讛爪诇讞讛. 讘讚讜拽 讗转 诇讜讙 讛砖讙讬讗讜转."
|
83 |
|
84 |
try:
|
85 |
+
# 讛转讗诪转 讛驻专讜诪驻讟 诇驻讜专诪讟 讛谞讻讜谉 诇驻讬 住讜讙 讛诪讜讚诇
|
86 |
+
if "mistral" in model_name.lower():
|
87 |
+
# 驻讜专诪讟 诇诪讜讚诇讬诐 诪住讜讙 Mistral
|
88 |
+
formatted_prompt = f"<s>[INST] {prompt} [/INST]"
|
89 |
+
outputs = generator(
|
90 |
+
formatted_prompt,
|
91 |
+
max_new_tokens=200,
|
92 |
+
do_sample=True,
|
93 |
+
temperature=0.7,
|
94 |
+
top_p=0.95,
|
95 |
+
return_full_text=False
|
96 |
+
)
|
97 |
+
else:
|
98 |
+
# 驻讜专诪讟 诇诪讜讚诇讬诐 诪住讜讙 Gemma 讜讚讜诪讬诐
|
99 |
+
outputs = generator(
|
100 |
+
[{"role": "user", "content": prompt}],
|
101 |
+
max_new_tokens=200,
|
102 |
+
do_sample=True,
|
103 |
+
temperature=0.7,
|
104 |
+
top_p=0.95,
|
105 |
+
return_full_text=False
|
106 |
+
)
|
107 |
+
|
108 |
+
# 诪讞讝讬专 讗转 讛讟拽住讟 砖谞讜爪专
|
109 |
+
if isinstance(outputs, list) and "generated_text" in outputs[0]:
|
110 |
+
return outputs[0]["generated_text"]
|
111 |
+
else:
|
112 |
+
return outputs[0]
|
113 |
except Exception as e:
|
114 |
return f"砖讙讬讗讛 讘讛驻注诇转 讛诪讜讚诇: {str(e)}"
|
115 |
|
116 |
# 讬爪讬专转 诪诪砖拽 诪砖转诪砖
|
117 |
with gr.Blocks() as demo:
|
118 |
+
gr.Markdown("# 爪'讗讟 讞讻诐 注诐 讙'诪讛")
|
119 |
gr.Markdown("讻转讜讘 砖讗诇讛 讗讜 讘拽砖讛 讜讛诪讜讚诇 讬注谞讛:")
|
120 |
|
121 |
with gr.Row():
|
|
|
133 |
[
|
134 |
"诪讛 讚注转讱 注诇 讘讬谞讛 诪诇讗讻讜转讬转?",
|
135 |
"讻转讜讘 住讬驻讜专 拽爪专 注诇 讞转讜诇 讜讻诇讘 砖讛诐 讞讘专讬诐",
|
136 |
+
"讛住讘专 诇讬 讗转 转讜专转 讛讬讞住讜转 讘爪讜专讛 驻砖讜讟讛",
|
137 |
+
"诪讛诐 讛讬转专讜谞讜转 砖诇 诇诪讬讚转 诪讻讜谞讛?"
|
138 |
],
|
139 |
input_text
|
140 |
)
|
141 |
|
142 |
+
gr.Markdown("""
|
143 |
+
### 讛注专讜转:
|
144 |
+
- 讛诪注专讻转 诪砖转诪砖转 讘诪讜讚诇 砖驻讛 诪转拽讚诐 诇注谞讜转 注诇 砖讗诇讜转
|
145 |
+
- 讛拽诇讬讚讜 砖讗诇讛 讗讜 讘拽砖讛 讜诇讞爪讜 注诇 '砖诇讞'
|
146 |
+
- 讛转砖讜讘讜转 谞讜爪专讜转 讘讝诪谉 讗诪转 讜讻诇 转砖讜讘讛 注砖讜讬讛 诇讛讬讜转 砖讜谞讛
|
147 |
+
""")
|
148 |
+
|
149 |
# 讛驻注诇转 讛诪诪砖拽
|
150 |
demo.launch()
|