Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,53 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
tokenizer
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
" \"js\": \"...JavaScript code...\"\n"
|
20 |
-
"}\n"
|
21 |
)
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
model_name = "sarvamai/sarvam-m"
|
6 |
+
|
7 |
+
# Load tokenizer and model
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_name, torch_dtype="auto", device_map="auto"
|
11 |
+
)
|
12 |
+
|
13 |
+
def generate_response(prompt):
|
14 |
+
messages = [{"role": "user", "content": prompt}]
|
15 |
+
text = tokenizer.apply_chat_template(
|
16 |
+
messages,
|
17 |
+
tokenize=False,
|
18 |
+
enable_thinking=True,
|
|
|
|
|
19 |
)
|
20 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
21 |
+
|
22 |
+
# Generate output with temperature=0.2
|
23 |
+
generated_ids = model.generate(
|
24 |
+
**model_inputs,
|
25 |
+
max_new_tokens=8192,
|
26 |
+
temperature=0.2
|
27 |
+
)
|
28 |
+
|
29 |
+
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
30 |
+
output_text = tokenizer.decode(output_ids)
|
31 |
+
|
32 |
+
if "</think>" in output_text:
|
33 |
+
reasoning_content = output_text.split("</think>")[0].rstrip("\n")
|
34 |
+
content = output_text.split("</think>")[-1].lstrip("\n").rstrip("</s>")
|
35 |
+
else:
|
36 |
+
reasoning_content = ""
|
37 |
+
content = output_text.rstrip("</s>")
|
38 |
+
|
39 |
+
return reasoning_content, content
|
40 |
+
|
41 |
+
# Gradio UI
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=generate_response,
|
44 |
+
inputs=gr.Textbox(lines=5, label="Enter your prompt"),
|
45 |
+
outputs=[
|
46 |
+
gr.Textbox(label="Reasoning"),
|
47 |
+
gr.Textbox(label="Response")
|
48 |
+
],
|
49 |
+
title="Sarvam-M Chat Interface",
|
50 |
+
description="Enter a prompt and receive both the internal reasoning and the final answer from the Sarvam-M model."
|
51 |
+
)
|
52 |
+
|
53 |
+
iface.launch()
|
|
|
|