Views
No views yet
1from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
2
3repo_id = "MahmutCanBoran/audi-insight-ai"
4tokenizer = MBart50TokenizerFast.from_pretrained(repo_id)
5tokenizer.src_lang = "en_XX"
6tokenizer.tgt_lang = "en_XX"
7
8model = MBartForConditionalGeneration.from_pretrained(repo_id)
9
10# Example input
11inp = "Audi A5 2.0 TFSI,I hear a rattling noise at startup."
12enc = tokenizer(inp, return_tensors="pt").to(model.device)
13
14gen = model.generate(
15 **enc,
16 max_new_tokens=64,
17 num_beams=4,
18 forced_bos_token_id=tokenizer.lang_code_to_id[tokenizer.tgt_lang]
19)
20
21print(tokenizer.decode(gen[0], skip_special_tokens=True))1
2💻 Example app.py
3import gradio as gr
4from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
5import torch
6
7# --- Load model & tokenizer ---
8REPO_ID = "MahmutCanBoran/audi-insight-ai"
9tokenizer = MBart50TokenizerFast.from_pretrained(REPO_ID)
10tokenizer.src_lang = "en_XX"
11model = MBartForConditionalGeneration.from_pretrained(REPO_ID)
12
13device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14model.to(device)
15
16# --- Diagnosis function ---
17def diagnose(user_text, num_beams, max_new_tokens):
18 txt = (user_text or "").strip()
19 if not txt:
20 return "Please type a symptom description like: `in my audi a5 2.0 tfsi i hear rattling noise at cold start`"
21
22 # Always output English
23 tokenizer.tgt_lang = "en_XX"
24
25 enc = tokenizer(txt, return_tensors="pt").to(device)
26 gen = model.generate(
27 **enc,
28 max_new_tokens=int(max_new_tokens),
29 num_beams=int(num_beams),
30 forced_bos_token_id=tokenizer.lang_code_to_id[tokenizer.tgt_lang]
31 )
32 out = tokenizer.decode(gen[0], skip_special_tokens=True)
33
34 return f"""### Result
35**Input**: `{txt}`
36
37**AI Agent Explanation:**
38{out}
39"""
40
41# --- Example text ---
42EXAMPLE_TEXT = "in my audi a5 2.0 tfsi i hear rattling noise at cold start"
43
44# --- Gradio UI ---
45with gr.Blocks(theme=gr.themes.Soft()) as demo:
46 gr.HTML(
47 """
48 <div style="text-align:center">
49 <h1 style="margin:10px 0 0 0">Audi Insight AI</h1>
50 <p>Type your full sentence (model + engine + symptom). Example:
51 <code>in my audi a5 2.0 tfsi i hear rattling noise</code></p>
52 </div>
53 """
54 )
55
56 with gr.Row():
57 with gr.Column(scale=2):
58 user_tb = gr.Textbox(label="Describe the issue", placeholder=EXAMPLE_TEXT, lines=3)
59
60 with gr.Accordion("Settings", open=False):
61 beams = gr.Slider(1, 8, value=4, step=1, label="Beam size")
62 max_tok = gr.Slider(16, 256, value=64, step=8, label="Max new tokens")
63
64 with gr.Row():
65 submit = gr.Button("Diagnose", variant="primary")
66 fill_ex = gr.Button("Use Example")
67 clear = gr.Button("Clear")
68
69 with gr.Column(scale=3):
70 output_md = gr.Markdown(value="### Result\n\n*(Awaiting input)*")
71
72 def fill_example():
73 return EXAMPLE_TEXT
74
75 def clear_all():
76 return "", "### Result\n\n*(Awaiting input)*"
77
78 submit.click(diagnose, inputs=[user_tb, beams, max_tok], outputs=[output_md])
79 fill_ex.click(fill_example, outputs=[user_tb])
80 clear.click(clear_all, outputs=[user_tb, output_md])
81
82# --- Launch app ---
83if __name__ == "__main__":
84 demo.launch()1🧭 Local Setup & Run
21. Clone repo
3git clone https://huggingface.co/spaces/MahmutCanBoran/audi-insight-ai-space
4cd audi-insight-ai-space
5
62. Install dependencies
7pip install -r requirements.txt
8
93. Run the app
10python app.py