Spaces:
Runtime error
Runtime error
initial commit
Browse filesinitial comit
Fix env
- .gitignore +1 -0
- README.md +2 -1
- app.py +30 -28
- requirements.txt +5 -1
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 💬
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
|
@@ -7,6 +7,7 @@ sdk: gradio
|
|
| 7 |
sdk_version: 5.0.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
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: Renj Portfolio Ai Bot
|
| 3 |
emoji: 💬
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
|
|
|
| 7 |
sdk_version: 5.0.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
short_description: Me as assistant to me
|
| 11 |
---
|
| 12 |
|
| 13 |
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,45 +1,47 @@
|
|
|
|
|
| 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 |
-
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 |
-
|
| 21 |
-
|
| 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 |
-
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
"""
|
| 44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 5 |
+
import torch
|
| 6 |
+
# Load your model and tokenizer
|
| 7 |
+
model_name = "Renjith95/renj-portfolio-finetuned-model" # Replace with your model name
|
| 8 |
+
auth_token = os.getenv("HF_TOKEN") # Get token from environment variable
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=auth_token)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, use_auth_token=auth_token)
|
| 11 |
+
|
| 12 |
|
| 13 |
"""
|
| 14 |
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
|
| 15 |
"""
|
| 16 |
+
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 17 |
|
| 18 |
|
| 19 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
+
for user_msg, assistant_msg in history:
|
| 22 |
+
messages.append({"role": "user", "content": user_msg})
|
| 23 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
messages.append({"role": "user", "content": message})
|
| 25 |
|
| 26 |
+
inputs = tokenizer.apply_chat_template(
|
|
|
|
|
|
|
| 27 |
messages,
|
| 28 |
+
tokenize=True,
|
| 29 |
+
add_generation_prompt=True,
|
| 30 |
+
return_tensors="pt"
|
| 31 |
+
).to(model.device)
|
| 32 |
+
|
| 33 |
+
outputs = model.generate(
|
| 34 |
+
input_ids=inputs,
|
| 35 |
+
max_new_tokens=max_tokens,
|
| 36 |
+
use_cache=True,
|
| 37 |
temperature=temperature,
|
| 38 |
top_p=top_p,
|
| 39 |
+
)
|
| 40 |
+
response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 41 |
+
|
| 42 |
+
# Assuming your model's response is the last part after the user's message
|
| 43 |
+
response = response.split(message)[-1].strip()
|
| 44 |
+
yield response
|
|
|
|
| 45 |
"""
|
| 46 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 47 |
"""
|
requirements.txt
CHANGED
|
@@ -1 +1,5 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
huggingface_hub>=0.20.0
|
| 3 |
+
transformers>=4.36.0
|
| 4 |
+
torch>=2.0.0
|
| 5 |
+
python-dotenv>=0.19.0
|