Spaces:
Runtime error
Runtime error
initial commit
Browse files- README.md +2 -1
- app.py +70 -51
- requirements.txt +0 -1
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: ElloraAI 2.0
|
3 |
emoji: 💬
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
|
|
7 |
sdk_version: 5.0.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: apache-2.0
|
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,64 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
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 |
-
|
|
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
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 |
-
|
40 |
-
|
41 |
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
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 |
|
63 |
-
|
64 |
-
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
2 |
+
from langchain.llms import HuggingFacePipeline
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain.chains import ConversationChain
|
5 |
+
from langchain.memory import ConversationBufferMemory
|
6 |
+
import torch
|
7 |
import gradio as gr
|
|
|
8 |
|
9 |
+
# Load the model and tokenizer
|
10 |
+
model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
11 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="cpu")
|
14 |
+
model = model.to(device)
|
15 |
|
16 |
+
# Create a Hugging Face pipeline
|
17 |
+
hf_pipeline = pipeline(
|
18 |
+
"text-generation",
|
19 |
+
model=model,
|
20 |
+
tokenizer=tokenizer,
|
21 |
+
device_map="cpu",
|
22 |
+
max_new_tokens=500, # Maximum number of tokens to generate
|
23 |
+
truncation=True, # Truncate input if it exceeds max_length
|
24 |
+
temperature=1.0, # Adjust this value based on your use case
|
25 |
+
return_full_text=False, # Do not include the input prompt in the output
|
26 |
+
)
|
27 |
|
28 |
+
# Wrap the pipeline in LangChain
|
29 |
+
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Define the system-level prompt template
|
32 |
+
template = """You are a helpful AI assistant and your name is Ellora mady by Abhishek sharma. Answer the user's questions clearly and concisely.
|
33 |
|
34 |
+
Conversation History:
|
35 |
+
{history}
|
36 |
|
37 |
+
User: {input}
|
38 |
+
Assistant:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
# Create the PromptTemplate
|
41 |
+
prompt = PromptTemplate(input_variables=["history", "input"], template=template)
|
42 |
|
43 |
+
# Add memory for conversation history
|
44 |
+
memory = ConversationBufferMemory()
|
45 |
|
46 |
+
# Create a chatbot chain
|
47 |
+
chatbot = ConversationChain(
|
48 |
+
llm=llm,
|
49 |
+
prompt=prompt,
|
50 |
+
memory=memory,
|
51 |
+
verbose=False # Set to True to see the prompt and memory in action
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
)
|
53 |
|
54 |
+
# Function to handle chatbot interaction
|
55 |
+
def chat(input_text):
|
56 |
+
response = chatbot.run(input_text)
|
57 |
+
return response
|
58 |
+
# Create a Gradio interface
|
59 |
+
def gradio_chat(input_text, history):
|
60 |
+
if input_text.lower() == "exit":
|
61 |
+
return "Chat ended."
|
62 |
+
response = chat(input_text)
|
63 |
+
history.append((input_text, response))
|
64 |
+
return history, history # Return updated history
|
65 |
+
|
66 |
+
# Gradio UI
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
gr.Markdown("## Ellora AI by Abhishek sharma")
|
69 |
+
chatbot_interface = gr.Chatbot(label="Chat History")
|
70 |
+
user_input = gr.Textbox(label="Your Question")
|
71 |
+
submit_button = gr.Button("Send")
|
72 |
+
clear_button = gr.Button("Clear")
|
73 |
+
|
74 |
+
# Define the interaction
|
75 |
+
submit_button.click(
|
76 |
+
gradio_chat,
|
77 |
+
inputs=[user_input, chatbot_interface],
|
78 |
+
outputs=[chatbot_interface, chatbot_interface]
|
79 |
+
)
|
80 |
+
clear_button.click(lambda: [], None, chatbot_interface, queue=False)
|
81 |
|
82 |
+
# Launch the Gradio app
|
83 |
+
demo.launch(pwa=True,share=True)
|
requirements.txt
CHANGED
@@ -1 +0,0 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|