AbdulWahab14 commited on
Commit
e2ef57f
·
verified ·
1 Parent(s): 59646d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -42
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- import requests
3
 
4
  # -----------------------------
5
  # Hugging Face API setup
@@ -10,46 +10,9 @@ if not HF_API_TOKEN:
10
  st.error("HF_API_TOKEN not found in Secrets.")
11
  st.stop()
12
 
13
- MODEL_ID = "HuggingFaceH4/zephyr-7b-beta"
14
- API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
15
- HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
16
-
17
- # -----------------------------
18
- # Helper function to query model
19
- # -----------------------------
20
- def query_hf_model(prompt, max_length=256):
21
- payload = {
22
- "inputs": prompt,
23
- "parameters": {
24
- "max_new_tokens": max_length,
25
- "temperature": 0.7,
26
- "return_full_text": False
27
- },
28
- }
29
-
30
- try:
31
- response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=120)
32
-
33
- if response.status_code == 200:
34
- result = response.json()
35
-
36
- if isinstance(result, list) and "generated_text" in result[0]:
37
- return result[0]["generated_text"].strip()
38
-
39
- if isinstance(result, dict) and "error" in result:
40
- return "Model error: " + result["error"]
41
-
42
- return str(result)
43
-
44
- elif response.status_code == 503:
45
- return "Model is loading. Please try again in a few seconds."
46
-
47
- else:
48
- return f"Request failed with status code {response.status_code}"
49
-
50
- except Exception as e:
51
- return f"Error: {str(e)}"
52
 
 
53
 
54
  # -----------------------------
55
  # Streamlit UI
@@ -70,8 +33,16 @@ user_input = st.text_input("Your Question:")
70
 
71
  if st.button("Submit") and user_input:
72
  with st.spinner("Generating AI answer..."):
73
- answer = query_hf_model(user_input)
74
- st.session_state.last_answer = answer
 
 
 
 
 
 
 
 
75
 
76
  # Display answer
77
  if st.session_state.last_answer:
 
1
  import streamlit as st
2
+ from huggingface_hub import InferenceClient
3
 
4
  # -----------------------------
5
  # Hugging Face API setup
 
10
  st.error("HF_API_TOKEN not found in Secrets.")
11
  st.stop()
12
 
13
+ client = InferenceClient(token=HF_API_TOKEN)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ MODEL_ID = "HuggingFaceH4/zephyr-7b-beta"
16
 
17
  # -----------------------------
18
  # Streamlit UI
 
33
 
34
  if st.button("Submit") and user_input:
35
  with st.spinner("Generating AI answer..."):
36
+ try:
37
+ response = client.text_generation(
38
+ prompt=user_input,
39
+ model=MODEL_ID,
40
+ max_new_tokens=200,
41
+ temperature=0.7,
42
+ )
43
+ st.session_state.last_answer = response
44
+ except Exception as e:
45
+ st.session_state.last_answer = f"Error: {str(e)}"
46
 
47
  # Display answer
48
  if st.session_state.last_answer: