Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,114 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
|
|
4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
return response[0]['generated_text']
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
|
|
|
|
|
1 |
+
|
2 |
+
import pandas as pd
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
5 |
|
6 |
+
# Initialize the InferenceClient
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
+
# Load your CSV file
|
10 |
+
df = pd.read_csv("your_file.csv")
|
11 |
+
|
12 |
+
# Create dropdowns for exam name, year, and problem number
|
13 |
+
exam_names = df["exam name"].unique()
|
14 |
+
year_options = df["year"].unique()
|
15 |
+
problem_numbers = df["problem number"].unique()
|
16 |
+
|
17 |
+
exam_dropdown = gr.Dropdown(exam_names, label="Exam Name")
|
18 |
+
year_dropdown = gr.Dropdown(year_options, label="Year")
|
19 |
+
problem_dropdown = gr.Dropdown(problem_numbers, label="Problem Number")
|
20 |
+
|
21 |
+
# Define the functions for the three buttons
|
22 |
+
def solve_problem(exam, year, problem):
|
23 |
+
problem_statement = df[(df["exam name"] == exam) & (df["year"] == year) & (df["problem number"] == problem)]["problem"].values[0]
|
24 |
+
prompt = f"Solve the following problem: {problem_statement}"
|
25 |
+
response = client.text_generation(prompt, max_new_tokens=512, temperature=0.7, top_p=0.95, model = "HuggingFaceH4/zephyr-7b-beta")
|
26 |
+
return response[0]['generated_text']
|
27 |
+
|
28 |
+
def give_hints(exam, year, problem):
|
29 |
+
problem_statement = df[(df["exam name"] == exam) & (df["year"] == year) & (df["problem number"] == problem)]["problem"].values[0]
|
30 |
+
prompt = f"Give hints for the following problem: {problem_statement}"
|
31 |
+
response = client.text_generation(prompt, max_new_tokens=512, temperature=0.7, top_p=0.95, model = "HuggingFaceH4/zephyr-7b-beta")
|
32 |
+
return response[0]['generated_text']
|
33 |
+
|
34 |
+
def create_similar_problem(exam, year, problem):
|
35 |
+
problem_statement = df[(df["exam name"] == exam) & (df["year"] == year) & (df["problem number"] == problem)]["problem"].values[0]
|
36 |
+
prompt = f"Create a similar problem to the following one: {problem_statement}"
|
37 |
+
response = client.text_generation(prompt, max_new_tokens=512, temperature=0.7, top_p=0.95, model = "HuggingFaceH4/zephyr-7b-beta")
|
38 |
return response[0]['generated_text']
|
39 |
|
40 |
+
# Define the chat response function
|
41 |
+
def respond(
|
42 |
+
message,
|
43 |
+
history: list[tuple[str, str]],
|
44 |
+
system_message,
|
45 |
+
max_tokens,
|
46 |
+
temperature,
|
47 |
+
top_p,
|
48 |
+
):
|
49 |
+
messages = [{"role": "system", "content": system_message}]
|
50 |
+
|
51 |
+
for val in history:
|
52 |
+
if val[0]:
|
53 |
+
messages.append({"role": "user", "content": val[0]})
|
54 |
+
if val[1]:
|
55 |
+
messages.append({"role": "assistant", "content": val[1]})
|
56 |
+
|
57 |
+
messages.append({"role": "user", "content": message})
|
58 |
+
|
59 |
+
response = ""
|
60 |
+
|
61 |
+
for message in client.chat_completion(
|
62 |
+
messages,
|
63 |
+
max_tokens=max_tokens,
|
64 |
+
stream=True,
|
65 |
+
temperature=temperature,
|
66 |
+
top_p=top_p,
|
67 |
+
):
|
68 |
+
token = message.choices[0].delta.content
|
69 |
+
|
70 |
+
response += token
|
71 |
+
yield response
|
72 |
+
|
73 |
+
# Create Gradio interface with Blocks context
|
74 |
+
with gr.Blocks() as dropdown_interface:
|
75 |
+
with gr.Column():
|
76 |
+
exam_dropdown.render()
|
77 |
+
year_dropdown.render()
|
78 |
+
problem_dropdown.render()
|
79 |
+
|
80 |
+
solve_button = gr.Button("Solve Problem")
|
81 |
+
hints_button = gr.Button("Give Hints")
|
82 |
+
similar_problem_button = gr.Button("Create Similar Problem")
|
83 |
+
|
84 |
+
output_text = gr.Textbox(label="Output")
|
85 |
+
|
86 |
+
solve_button.click(solve_problem, inputs=[exam_dropdown, year_dropdown, problem_dropdown], outputs=output_text)
|
87 |
+
hints_button.click(give_hints, inputs=[exam_dropdown, year_dropdown, problem_dropdown], outputs=output_text)
|
88 |
+
similar_problem_button.click(create_similar_problem, inputs=[exam_dropdown, year_dropdown, problem_dropdown], outputs=output_text)
|
89 |
+
|
90 |
+
chat_interface = gr.ChatInterface(
|
91 |
+
respond,
|
92 |
+
additional_inputs=[
|
93 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
94 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
95 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
96 |
+
gr.Slider(
|
97 |
+
minimum=0.1,
|
98 |
+
maximum=1.0,
|
99 |
+
value=0.95,
|
100 |
+
step=0.05,
|
101 |
+
label="Top-p (nucleus sampling)",
|
102 |
+
),
|
103 |
+
],
|
104 |
+
)
|
105 |
+
|
106 |
+
# Combine both interfaces into a tabbed layout
|
107 |
+
tabbed_interface = gr.TabbedInterface(
|
108 |
+
[dropdown_interface, chat_interface],
|
109 |
+
["Problem Solver", "Chat Interface"]
|
110 |
+
)
|
111 |
|
112 |
+
# Launch the app
|
113 |
+
if __name__ == "__main__":
|
114 |
+
tabbed_interface.launch()
|