Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +67 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import transformers
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load the chatbot model
|
6 |
+
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
7 |
+
pipeline = transformers.pipeline(
|
8 |
+
"text-generation",
|
9 |
+
model=model_id,
|
10 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
11 |
+
device_map="auto",
|
12 |
+
)
|
13 |
+
|
14 |
+
# Function to calculate scores and rankings
|
15 |
+
def calculate_ranking(data):
|
16 |
+
for institution in data:
|
17 |
+
institution["Total"] = (
|
18 |
+
institution["TLR"] + institution["GO"] + institution["OI"] + institution["PR"]
|
19 |
+
)
|
20 |
+
ranked_data = sorted(data, key=lambda x: x["Total"], reverse=True)
|
21 |
+
for rank, institution in enumerate(ranked_data, start=1):
|
22 |
+
institution["Rank"] = rank
|
23 |
+
return ranked_data
|
24 |
+
|
25 |
+
# Chatbot function with ranking logic
|
26 |
+
def chatbot_response(user_message):
|
27 |
+
if "rank" in user_message.lower():
|
28 |
+
# Example data for ranking
|
29 |
+
example_data = [
|
30 |
+
{"Institution": "A", "TLR": 70, "GO": 85, "OI": 90, "PR": 75},
|
31 |
+
{"Institution": "B", "TLR": 80, "GO": 88, "OI": 85, "PR": 90},
|
32 |
+
{"Institution": "C", "TLR": 65, "GO": 80, "OI": 70, "PR": 60},
|
33 |
+
]
|
34 |
+
ranked_data = calculate_ranking(example_data)
|
35 |
+
response = "Here are the ranks of the institutions:\n"
|
36 |
+
for institution in ranked_data:
|
37 |
+
response += f"Rank {institution['Rank']}: {institution['Institution']} (Total Score: {institution['Total']})\n"
|
38 |
+
return response
|
39 |
+
else:
|
40 |
+
# Generate chatbot response from model
|
41 |
+
outputs = pipeline(
|
42 |
+
user_message,
|
43 |
+
max_new_tokens=256,
|
44 |
+
do_sample=True,
|
45 |
+
temperature=0.6,
|
46 |
+
top_p=0.9,
|
47 |
+
)
|
48 |
+
return outputs[0]["generated_text"]
|
49 |
+
|
50 |
+
# Gradio interface
|
51 |
+
def build_gradio_ui():
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown("## Chatbot with Hugging Face Spaces")
|
54 |
+
gr.Markdown("Type a message to interact with the chatbot! (Ask about institution rankings too!)")
|
55 |
+
with gr.Row():
|
56 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
57 |
+
chatbot_output = gr.Textbox(label="Chatbot Response", interactive=False)
|
58 |
+
submit_button = gr.Button("Send")
|
59 |
+
submit_button.click(chatbot_response, inputs=[user_input], outputs=[chatbot_output])
|
60 |
+
|
61 |
+
return demo
|
62 |
+
|
63 |
+
# Launch the Gradio app
|
64 |
+
demo = build_gradio_ui()
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|