joermd commited on
Commit
b1c5080
Β·
verified Β·
1 Parent(s): 8854aa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -23
app.py CHANGED
@@ -1,15 +1,13 @@
1
  import numpy as np
2
  import streamlit as st
 
 
3
  import os
4
- from dotenv import load_dotenv
5
- import requests
6
 
7
- # Load environment variables
8
- load_dotenv()
9
-
10
- # Hugging Face API URL and token
11
- HUGGINGFACE_API_URL = ["joermd/llma-speedy"]
12
- HUGGINGFACE_API_TOKEN = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
13
 
14
  # Random dog images for error messages
15
  random_dog = [
@@ -30,7 +28,6 @@ max_token_value = st.sidebar.slider('Select a max_token value', 1000, 9000, 5000
30
  st.sidebar.button('Reset Chat', on_click=reset_conversation)
31
 
32
  # Set the model and display its name
33
- model_name = "joermd/llma-speedy"
34
  st.sidebar.write(f"You're now chatting with **{model_name}**")
35
  st.sidebar.markdown("*Generated content may be inaccurate or false.*")
36
 
@@ -52,20 +49,14 @@ if prompt := st.chat_input(f"Hi, I'm {model_name}, ask me a question"):
52
  # Display assistant response
53
  with st.chat_message("assistant"):
54
  try:
55
- headers = {"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"}
56
- payload = {
57
- "inputs": prompt,
58
- "parameters": {"temperature": temp_values, "max_new_tokens": max_token_value}
59
- }
60
- response = requests.post(HUGGINGFACE_API_URL, headers=headers, json=payload)
61
-
62
- if response.status_code == 200:
63
- result = response.json()
64
- assistant_response = result.get("generated_text", "No response generated.")
65
- else:
66
- assistant_response = "Error: Unable to reach the model."
67
- st.write(f"Status Code: {response.status_code}")
68
-
69
  except Exception as e:
70
  assistant_response = "πŸ˜΅β€πŸ’« Connection issue! Try again later. Here's a 🐢:"
71
  st.image(f'https://random.dog/{random_dog[np.random.randint(len(random_dog))]}')
 
1
  import numpy as np
2
  import streamlit as st
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
  import os
 
 
6
 
7
+ # Load the model and tokenizer
8
+ model_name = "joermd/llma-speedy"
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
11
 
12
  # Random dog images for error messages
13
  random_dog = [
 
28
  st.sidebar.button('Reset Chat', on_click=reset_conversation)
29
 
30
  # Set the model and display its name
 
31
  st.sidebar.write(f"You're now chatting with **{model_name}**")
32
  st.sidebar.markdown("*Generated content may be inaccurate or false.*")
33
 
 
49
  # Display assistant response
50
  with st.chat_message("assistant"):
51
  try:
52
+ inputs = tokenizer(prompt, return_tensors="pt")
53
+ outputs = model.generate(
54
+ inputs.input_ids,
55
+ max_new_tokens=max_token_value,
56
+ temperature=temp_values,
57
+ do_sample=True
58
+ )
59
+ assistant_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
60
  except Exception as e:
61
  assistant_response = "πŸ˜΅β€πŸ’« Connection issue! Try again later. Here's a 🐢:"
62
  st.image(f'https://random.dog/{random_dog[np.random.randint(len(random_dog))]}')