akhaliq HF Staff commited on
Commit
e8c1290
·
verified ·
1 Parent(s): b63ffc2

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+
5
+ # Initialize the model pipeline
6
+ model_id = "facebook/MobileLLM-R1-950M"
7
+ pipe = pipeline(
8
+ "text-generation",
9
+ model=model_id,
10
+ torch_dtype="auto",
11
+ device_map="auto",
12
+ )
13
+
14
+ def respond(message, history):
15
+ # Convert history to messages format
16
+ messages = []
17
+
18
+ # Add conversation history
19
+ for user_msg, assistant_msg in history:
20
+ if user_msg:
21
+ messages.append({"role": "user", "content": user_msg})
22
+ if assistant_msg:
23
+ messages.append({"role": "assistant", "content": assistant_msg})
24
+
25
+ # Add current message
26
+ messages.append({"role": "user", "content": message})
27
+
28
+ # Generate response
29
+ outputs = pipe(
30
+ messages,
31
+ max_new_tokens=512,
32
+ temperature=0.7,
33
+ do_sample=True,
34
+ pad_token_id=pipe.tokenizer.eos_token_id,
35
+ )
36
+
37
+ # Extract only the assistant's response
38
+ generated_text = outputs[0]["generated_text"]
39
+ assistant_response = generated_text[-1]["content"]
40
+
41
+ return assistant_response
42
+
43
+ # Create the chat interface
44
+ demo = gr.ChatInterface(
45
+ fn=respond,
46
+ title="MobileLLM Chat",
47
+ description="Chat with Facebook's MobileLLM-R1-950M model",
48
+ examples=[
49
+ "Write a Python function that returns the square of a number.",
50
+ "Compute: 1-2+3-4+5- ... +99-100.",
51
+ "Write a C++ program that prints 'Hello, World!'.",
52
+ "Explain how recursion works in programming.",
53
+ "What is the difference between a list and a tuple in Python?",
54
+ ],
55
+ retry_btn=None,
56
+ undo_btn=None,
57
+ clear_btn="Clear",
58
+ theme=gr.themes.Soft(),
59
+ )
60
+
61
+ if __name__ == "__main__":
62
+ demo.launch()