yuvrajpant56 commited on
Commit
2ce8469
·
verified ·
1 Parent(s): 15774b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -53
app.py CHANGED
@@ -1,58 +1,21 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- import os
4
 
5
- # Replace with your model's repo
6
- client = InferenceClient("yuvrajpant56/Mistral_Posttrain_SFT",token=os.environ["HF_TOKEN"])
7
 
8
- def respond(
9
- message,
10
- history: list[tuple[str, str]],
11
- system_message,
12
- max_tokens,
13
- temperature,
14
- top_p,
15
- ):
16
- # Build simple prompt using system message and conversation history
17
- prompt = f"{system_message.strip()}\n\n"
18
 
19
- for user_msg, bot_msg in history:
20
- if user_msg:
21
- prompt += f"User: {user_msg}\n"
22
- if bot_msg:
23
- prompt += f"Assistant: {bot_msg}\n"
24
- prompt += f"User: {message}\nAssistant:"
25
 
26
- response = ""
27
- for token in client.text_generation(
28
- prompt,
29
- max_new_tokens=max_tokens,
30
- temperature=temperature,
31
- top_p=top_p,
32
- stream=True,
33
- ):
34
- response += token.token
35
- yield response
36
-
37
- # Build Gradio ChatInterface
38
- demo = gr.ChatInterface(
39
- fn=respond,
40
- additional_inputs=[
41
- gr.Textbox(value="You are a helpful assistant.", label="System message"),
42
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
43
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
44
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
45
- ],
46
- title="🧠 Mistral SFT Chatbot",
47
- description="Interact with your fine-tuned Mistral model using Hugging Face Inference API.",
48
- theme="soft",
49
- examples=[
50
- ["Tell me a story about the Himalayas.", "You are a helpful assistant.", 512, 0.7, 0.95],
51
- ["What is quantum computing?", "Explain like I’m 5.", 512, 0.8, 0.9]
52
- ],
53
- type="openai", # also removes the warning about 'tuples'
54
- )
55
-
56
-
57
- if __name__ == "__main__":
58
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
+ model_id = "yuvrajpant56/Mistral_Posttrain_SFT"
 
6
 
7
+ # Load model & tokenizer
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
 
 
 
 
 
 
 
10
 
11
+ def generate_text(prompt):
12
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
13
+ outputs = model.generate(**inputs, max_new_tokens=100)
14
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
15
 
16
+ gr.Interface(fn=generate_text,
17
+ inputs="text",
18
+ outputs="text",
19
+ title="Mistral SFT Text Generator",
20
+ description="Type a prompt and let the fine-tuned Mistral model generate the rest."
21
+ ).launch()