Spaces:
Sleeping
Sleeping
dylanonfb
commited on
Commit
·
6db0360
1
Parent(s):
99d47f8
Updated.
Browse files- README.md +2 -2
- app.py +79 -23
- requirements.txt +75 -1
README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
---
|
2 |
-
title: Llama
|
3 |
emoji: 💬
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
@@ -8,7 +8,7 @@ sdk_version: 5.0.1
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
-
short_description:
|
12 |
---
|
13 |
|
14 |
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
|
|
1 |
---
|
2 |
+
title: Llama Ask
|
3 |
emoji: 💬
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
+
short_description: Testing context-na
|
12 |
---
|
13 |
|
14 |
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
app.py
CHANGED
@@ -1,32 +1,81 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
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":
|
27 |
|
28 |
response = ""
|
29 |
-
|
30 |
for message in client.chat_completion(
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
@@ -37,26 +86,33 @@ def respond(
|
|
37 |
token = message.choices[0].delta.content
|
38 |
|
39 |
response += token
|
40 |
-
|
|
|
|
|
41 |
|
42 |
|
43 |
"""
|
44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
demo = gr.ChatInterface(
|
47 |
-
respond
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
5 |
+
import transformers
|
6 |
+
import torch
|
7 |
+
|
8 |
+
from google.cloud import translate_v2 as translate
|
9 |
+
# Load the credentials from the secret
|
10 |
+
credentials = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
11 |
+
|
12 |
+
# Write the credentials to a temporary file
|
13 |
+
credentials_path = "google_credentials.json"
|
14 |
+
|
15 |
+
with open(credentials_path, "w") as f:
|
16 |
+
f.write(credentials)
|
17 |
+
|
18 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path
|
19 |
+
|
20 |
+
def translate_text(source:str, target: str, text: str) -> dict:
|
21 |
+
"""Translates text into the target language.
|
22 |
+
|
23 |
+
Target must be an ISO 639-1 language code.
|
24 |
+
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
|
25 |
+
"""
|
26 |
+
|
27 |
+
translate_client = translate.Client()
|
28 |
|
29 |
+
if isinstance(text, bytes):
|
30 |
+
text = text.decode("utf-8")
|
31 |
+
|
32 |
+
# Text can also be a sequence of strings, in which case this method
|
33 |
+
# will return a sequence of results for each text.
|
34 |
+
result = translate_client.translate(text, source_language=source,target_language=target)
|
35 |
+
# print(result)
|
36 |
+
# print("Text: {}".format(result["input"]))
|
37 |
+
# print("Translation: {}".format(result["translatedText"]))
|
38 |
+
# # print("Detected source language: {}".format(result["detectedSourceLanguage"]))
|
39 |
+
|
40 |
+
return result
|
41 |
"""
|
42 |
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
|
43 |
"""
|
44 |
+
model_id="chuanli11/Llama-3.2-3B-Instruct-uncensored"
|
45 |
+
client = InferenceClient(model_id)
|
46 |
|
47 |
+
pipeline = transformers.pipeline(
|
48 |
+
"text-generation",
|
49 |
+
model=model_id,
|
50 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
51 |
+
device_map="auto",
|
52 |
+
)
|
53 |
|
54 |
def respond(
|
55 |
message,
|
56 |
history: list[tuple[str, str]],
|
57 |
+
system_message="You are a friendly Chatbot.",
|
58 |
+
max_tokens=2048,
|
59 |
+
temperature=0.7,
|
60 |
+
top_p=0.95
|
61 |
):
|
62 |
+
print(f"Input...{message}")
|
63 |
+
tmp_english_out_text = translate_text("mni-Mtei","en",message)["translatedText"]
|
64 |
+
|
65 |
+
print(f"Translated to English...{tmp_english_out_text}")
|
66 |
+
|
67 |
messages = [{"role": "system", "content": system_message}]
|
68 |
|
69 |
for val in history:
|
70 |
if val[0]:
|
71 |
+
messages.append({"role": "user", "content": translate_text("mni-Mtei","en",val[0])["translatedText"]})
|
72 |
if val[1]:
|
73 |
+
messages.append({"role": "assistant", "content": translate_text("mni-Mtei","en",val[1])["translatedText"]})
|
74 |
|
75 |
+
messages.append({"role": "user", "content": tmp_english_out_text})
|
76 |
|
77 |
response = ""
|
78 |
+
print(f"Running inference...{messages}")
|
79 |
for message in client.chat_completion(
|
80 |
messages,
|
81 |
max_tokens=max_tokens,
|
|
|
86 |
token = message.choices[0].delta.content
|
87 |
|
88 |
response += token
|
89 |
+
print(f"Response...{response}")
|
90 |
+
print(f"Yield {translate_text('en','mni-Mtei',response)}")
|
91 |
+
yield translate_text("en","mni-Mtei",response)["translatedText"]
|
92 |
|
93 |
|
94 |
"""
|
95 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
96 |
"""
|
97 |
+
# demo = gr.ChatInterface(
|
98 |
+
# respond,
|
99 |
+
# additional_inputs=[
|
100 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
101 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
102 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
103 |
+
# gr.Slider(
|
104 |
+
# minimum=0.1,
|
105 |
+
# maximum=1.0,
|
106 |
+
# value=0.95,
|
107 |
+
# step=0.05,
|
108 |
+
# label="Top-p (nucleus sampling)",
|
109 |
+
# ),
|
110 |
+
# ],
|
111 |
+
# )
|
112 |
+
|
113 |
+
|
114 |
demo = gr.ChatInterface(
|
115 |
+
respond
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
)
|
117 |
|
118 |
|
requirements.txt
CHANGED
@@ -1 +1,75 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
accelerate==1.2.1
|
3 |
+
aiofiles==23.2.1
|
4 |
+
annotated-types==0.7.0
|
5 |
+
anyio==4.7.0
|
6 |
+
cachetools==4.2.4
|
7 |
+
certifi==2024.12.14
|
8 |
+
charset-normalizer==3.4.0
|
9 |
+
click==8.1.7
|
10 |
+
fastapi==0.115.6
|
11 |
+
ffmpy==0.5.0
|
12 |
+
filelock==3.16.1
|
13 |
+
fsspec==2024.12.0
|
14 |
+
google-api-core==1.34.1
|
15 |
+
google-auth==1.35.0
|
16 |
+
google-cloud-core==1.7.3
|
17 |
+
google-cloud-translate==2.0.1
|
18 |
+
googleapis-common-protos==1.66.0
|
19 |
+
gradio==5.9.1
|
20 |
+
gradio_client==1.5.2
|
21 |
+
grpcio==1.68.1
|
22 |
+
grpcio-status==1.48.2
|
23 |
+
h11==0.14.0
|
24 |
+
httpcore==1.0.7
|
25 |
+
httpx==0.28.1
|
26 |
+
huggingface-hub==0.25.2
|
27 |
+
idna==3.10
|
28 |
+
Jinja2==3.1.4
|
29 |
+
markdown-it-py==3.0.0
|
30 |
+
MarkupSafe==2.1.5
|
31 |
+
mdurl==0.1.2
|
32 |
+
mpmath==1.3.0
|
33 |
+
networkx==3.4.2
|
34 |
+
numpy==2.2.0
|
35 |
+
orjson==3.10.12
|
36 |
+
packaging==24.2
|
37 |
+
pandas==2.2.3
|
38 |
+
pillow==11.0.0
|
39 |
+
protobuf==3.20.3
|
40 |
+
psutil==6.1.1
|
41 |
+
pyasn1==0.6.1
|
42 |
+
pyasn1_modules==0.4.1
|
43 |
+
pydantic==2.10.4
|
44 |
+
pydantic_core==2.27.2
|
45 |
+
pydub==0.25.1
|
46 |
+
Pygments==2.18.0
|
47 |
+
python-dateutil==2.9.0.post0
|
48 |
+
python-multipart==0.0.20
|
49 |
+
pytz==2024.2
|
50 |
+
PyYAML==6.0.2
|
51 |
+
regex==2024.11.6
|
52 |
+
requests==2.32.3
|
53 |
+
rich==13.9.4
|
54 |
+
rsa==4.9
|
55 |
+
ruff==0.8.4
|
56 |
+
safehttpx==0.1.6
|
57 |
+
safetensors==0.4.5
|
58 |
+
semantic-version==2.10.0
|
59 |
+
setuptools==75.6.0
|
60 |
+
shellingham==1.5.4
|
61 |
+
six==1.17.0
|
62 |
+
sniffio==1.3.1
|
63 |
+
starlette==0.41.3
|
64 |
+
sympy==1.13.1
|
65 |
+
tokenizers==0.21.0
|
66 |
+
tomlkit==0.13.2
|
67 |
+
torch==2.5.1
|
68 |
+
tqdm==4.67.1
|
69 |
+
transformers==4.47.1
|
70 |
+
typer==0.15.1
|
71 |
+
typing_extensions==4.12.2
|
72 |
+
tzdata==2024.2
|
73 |
+
urllib3==2.2.3
|
74 |
+
uvicorn==0.34.0
|
75 |
+
websockets==14.1
|