Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,34 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
from datasets import load_dataset
|
4 |
|
5 |
-
# Load the PleIAs/common_corpus dataset
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
|
|
|
|
9 |
def get_example_from_corpus(dataset, index):
|
10 |
-
if "train" in dataset:
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
13 |
else:
|
14 |
-
|
15 |
|
16 |
-
# Initialize
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def respond(
|
20 |
message,
|
@@ -24,33 +38,37 @@ def respond(
|
|
24 |
temperature,
|
25 |
top_p,
|
26 |
):
|
|
|
|
|
|
|
27 |
messages = [{"role": "system", "content": system_message}]
|
28 |
|
29 |
-
# Add historical interactions
|
30 |
for val in history:
|
31 |
if val[0]:
|
32 |
messages.append({"role": "user", "content": val[0]})
|
33 |
if val[1]:
|
34 |
messages.append({"role": "assistant", "content": val[1]})
|
35 |
|
36 |
-
# Add user's message
|
37 |
messages.append({"role": "user", "content": message})
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
return response
|
48 |
|
49 |
-
# Example
|
50 |
-
example_data = get_example_from_corpus(common_corpus,
|
51 |
print("Example from PleIAs/common_corpus:", example_data)
|
52 |
|
53 |
-
# Gradio
|
54 |
demo = gr.ChatInterface(
|
55 |
respond,
|
56 |
additional_inputs=[
|
@@ -68,4 +86,7 @@ demo = gr.ChatInterface(
|
|
68 |
)
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
-
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from datasets import load_dataset
|
4 |
|
5 |
+
# Load the PleIAs/common_corpus dataset with error handling
|
6 |
+
def load_common_corpus():
|
7 |
+
try:
|
8 |
+
return load_dataset("PleIAs/common_corpus")
|
9 |
+
except Exception as e:
|
10 |
+
print(f"Error loading dataset: {e}")
|
11 |
+
return None
|
12 |
|
13 |
+
common_corpus = load_common_corpus()
|
14 |
+
|
15 |
+
# Retrieve an example from the dataset safely
|
16 |
def get_example_from_corpus(dataset, index):
|
17 |
+
if dataset and "train" in dataset:
|
18 |
+
try:
|
19 |
+
return dataset["train"][index]
|
20 |
+
except IndexError:
|
21 |
+
print("Index out of range for dataset")
|
22 |
+
return {"text": "No example available"}
|
23 |
else:
|
24 |
+
return {"text": "Dataset not loaded correctly"}
|
25 |
|
26 |
+
# Initialize the Inference Client with error handling
|
27 |
+
try:
|
28 |
+
client = InferenceClient("unsloth/Llama-3.2-1B-Instruct")
|
29 |
+
except Exception as e:
|
30 |
+
print(f"Error initializing inference client: {e}")
|
31 |
+
client = None
|
32 |
|
33 |
def respond(
|
34 |
message,
|
|
|
38 |
temperature,
|
39 |
top_p,
|
40 |
):
|
41 |
+
if not client:
|
42 |
+
return "Error: Inference client not initialized."
|
43 |
+
|
44 |
messages = [{"role": "system", "content": system_message}]
|
45 |
|
|
|
46 |
for val in history:
|
47 |
if val[0]:
|
48 |
messages.append({"role": "user", "content": val[0]})
|
49 |
if val[1]:
|
50 |
messages.append({"role": "assistant", "content": val[1]})
|
51 |
|
|
|
52 |
messages.append({"role": "user", "content": message})
|
53 |
|
54 |
+
try:
|
55 |
+
response = client.chat_completion(
|
56 |
+
messages,
|
57 |
+
max_tokens=max_tokens,
|
58 |
+
temperature=temperature,
|
59 |
+
top_p=top_p,
|
60 |
+
).choices[0].message.content
|
61 |
+
except Exception as e:
|
62 |
+
print(f"Error during inference: {e}")
|
63 |
+
response = "An error occurred while generating a response."
|
64 |
+
|
65 |
return response
|
66 |
|
67 |
+
# Example: Retrieve an entry from the dataset to demonstrate integration
|
68 |
+
example_data = get_example_from_corpus(common_corpus, 0)
|
69 |
print("Example from PleIAs/common_corpus:", example_data)
|
70 |
|
71 |
+
# Gradio interface with proper error handling
|
72 |
demo = gr.ChatInterface(
|
73 |
respond,
|
74 |
additional_inputs=[
|
|
|
86 |
)
|
87 |
|
88 |
if __name__ == "__main__":
|
89 |
+
try:
|
90 |
+
demo.launch()
|
91 |
+
except Exception as e:
|
92 |
+
print(f"Error launching Gradio app: {e}")
|