Euryeth commited on
Commit
beb9a26
·
verified ·
1 Parent(s): c60c816

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -42
app.py CHANGED
@@ -3,53 +3,37 @@ from transformers import pipeline
3
  import torch
4
  import os
5
 
6
- # Configure cache to avoid space limitations
7
- os.environ['HF_HOME'] = '/tmp/cache'
8
 
9
- # Use a reliable LLM hosted by Hugging Face
10
- MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
11
 
12
  # Load the model pipeline
13
  generator = pipeline(
14
  "text-generation",
15
- model=MODEL_NAME,
16
- device_map="auto",
17
- torch_dtype=torch.bfloat16,
18
- max_new_tokens=560
19
  )
20
 
21
- def generate_chat_completion(message_history, max_tokens=560, temperature=0.8):
22
- """Generate assistant response from chat message history"""
23
- try:
24
- # If using Gradio chat format (list of tuples), convert to role-content dicts
25
- messages = [{"role": "user", "content": msg} if i % 2 == 0 else {"role": "assistant", "content": msg}
26
- for i, msg in enumerate(message_history)]
27
-
28
- prompt = "\n".join([f"{m['role'].capitalize()}: {m['content']}" for m in messages])
29
- prompt += "\nAssistant:"
30
-
31
- output = generator(
32
- prompt,
33
- max_new_tokens=max_tokens,
34
- temperature=temperature,
35
- top_p=0.95,
36
- repetition_penalty=1.15,
37
- do_sample=True
38
- )
39
- response = output[0]['generated_text'].replace(prompt, "").strip()
40
- return message_history + [response]
41
- except Exception as e:
42
- return message_history + [f"[Error] {str(e)}"]
43
-
44
- # Gradio Chat Interface
45
- chat_interface = gr.ChatInterface(
46
- fn=generate_chat_completion,
47
- title="Mistral-7B Chat",
48
- description="Powered by Hugging Face Transformers",
49
- retry_btn="Retry",
50
- undo_btn="Undo",
51
- clear_btn="Clear"
52
- )
53
 
54
- if __name__ == "__main__":
55
- chat_interface.launch()
 
 
 
 
 
3
  import torch
4
  import os
5
 
6
+ # Use safe float32 for CPU compatibility
7
+ torch_dtype = torch.float32
8
 
9
+ # Configure cache directory
10
+ os.environ['HF_HOME'] = '/tmp/cache'
11
 
12
  # Load the model pipeline
13
  generator = pipeline(
14
  "text-generation",
15
+ model="mistralai/Mistral-7B-Instruct-v0.2",
16
+ device=0 if torch.cuda.is_available() else -1,
17
+ torch_dtype=torch_dtype
 
18
  )
19
 
20
+ def generate_chat_completion(message, history):
21
+ """Basic chatbot for Gradio interface"""
22
+ prompt = f"User: {message}\nAssistant:"
23
+ output = generator(
24
+ prompt,
25
+ max_new_tokens=512,
26
+ temperature=0.8,
27
+ top_p=0.95,
28
+ repetition_penalty=1.15,
29
+ do_sample=True
30
+ )
31
+ response = output[0]['generated_text'].replace(prompt, "").strip()
32
+ return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ gr.ChatInterface(fn=generate_chat_completion,
35
+ title="Mistral Chatbot",
36
+ description="Chat with Mistral-7B",
37
+ retry_btn="Retry",
38
+ undo_btn="Undo",
39
+ clear_btn="Clear").launch()