Euryeth commited on
Commit
f2bc959
·
verified ·
1 Parent(s): 17aeec1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -16
app.py CHANGED
@@ -1,14 +1,16 @@
1
- import gradio as gr
2
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
  import os
5
-
6
  from huggingface_hub import login
 
 
7
  login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
8
 
 
9
  torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
10
  os.environ['HF_HOME'] = '/tmp/cache'
11
 
 
12
  model_name = "tiiuae/falcon-rw-1b"
13
 
14
  tokenizer = AutoTokenizer.from_pretrained(model_name)
@@ -18,6 +20,7 @@ model = AutoModelForCausalLM.from_pretrained(
18
  device_map="auto"
19
  )
20
 
 
21
  generator = pipeline(
22
  "text-generation",
23
  model=model,
@@ -26,10 +29,11 @@ generator = pipeline(
26
  torch_dtype=torch_dtype
27
  )
28
 
 
29
  def generate_chat_completion(message: str, history: list = None):
30
  """
31
- If history is provided as list of [role,content] pairs,
32
- reconstruct prompt. Returns updated history list.
33
  """
34
  history = history or []
35
  prompt = ""
@@ -47,18 +51,7 @@ def generate_chat_completion(message: str, history: list = None):
47
  )
48
  reply = output[0]['generated_text'].replace(prompt, "").strip()
49
 
50
- # Append to history
51
  history.append({"role": "user", "content": message})
52
  history.append({"role": "assistant", "content": reply})
53
-
54
  return history
55
-
56
- # Adapt Gradio to pass/receive history automatically
57
- gr.ChatInterface(
58
- fn=generate_chat_completion,
59
- title="Falcon Chatbot",
60
- description="Roleplay-ready chat using Falcon-RW‑1B",
61
- retry_btn="Retry",
62
- undo_btn="Undo",
63
- clear_btn="Clear"
64
- ).launch()
 
 
1
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
2
  import torch
3
  import os
 
4
  from huggingface_hub import login
5
+
6
+ # Authenticate with Hugging Face token
7
  login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
8
 
9
+ # Setup environment
10
  torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
11
  os.environ['HF_HOME'] = '/tmp/cache'
12
 
13
+ # Load model and tokenizer
14
  model_name = "tiiuae/falcon-rw-1b"
15
 
16
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
20
  device_map="auto"
21
  )
22
 
23
+ # Create text generation pipeline
24
  generator = pipeline(
25
  "text-generation",
26
  model=model,
 
29
  torch_dtype=torch_dtype
30
  )
31
 
32
+ # Main function for generating chat completions
33
  def generate_chat_completion(message: str, history: list = None):
34
  """
35
+ If history is provided as list of {'role': str, 'content': str} dicts,
36
+ it reconstructs the full prompt and returns updated history.
37
  """
38
  history = history or []
39
  prompt = ""
 
51
  )
52
  reply = output[0]['generated_text'].replace(prompt, "").strip()
53
 
54
+ # Return updated conversation history
55
  history.append({"role": "user", "content": message})
56
  history.append({"role": "assistant", "content": reply})
 
57
  return history