windowvomit commited on
Commit
c1ecd28
·
verified ·
1 Parent(s): f568802

made API legal and hidden

Browse files

bro jus tread the title of the commit

Files changed (1) hide show
  1. app.py +36 -23
app.py CHANGED
@@ -1,34 +1,47 @@
 
 
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
 
4
- # Load a lightweight GPT model
5
- model_name = "EleutherAI/gpt-neo-125M"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForCausalLM.from_pretrained(model_name)
 
8
 
9
  chat_history = []
10
 
11
  def chat(user_message):
12
- chat_history.append(f"You: {user_message}")
13
- prompt = "\n".join(chat_history) + "\nAI:"
14
- inputs = tokenizer(prompt, return_tensors="pt")
15
- outputs = model.generate(
16
- **inputs,
17
- max_length=inputs["input_ids"].shape[1] + 60,
18
- pad_token_id=tokenizer.eos_token_id,
19
- do_sample=True,
20
- top_k=50,
21
- top_p=0.95,
22
- temperature=0.7,
23
- )
24
- full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
25
- reply = full_output[len(prompt):].strip()
26
- chat_history.append(f"AI: {reply}")
 
 
 
 
 
 
27
  return reply
28
 
29
  with gr.Blocks() as demo:
30
  chatbot_ui = gr.Chatbot()
31
- msg = gr.Textbox(placeholder="Type a message and press Enter")
32
- msg.submit(chat, msg, chatbot_ui)
 
 
 
 
33
 
34
- demo.launch()
 
 
1
+ import os
2
+ import requests
3
  import gradio as gr
 
4
 
5
+ API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-125M"
6
+
7
+ HEADERS = {
8
+ "Authorization": f"Bearer {os.environ['HUGGINGFACEHUB_API_TOKEN']}"
9
+ }
10
 
11
  chat_history = []
12
 
13
  def chat(user_message):
14
+ chat_history.append({"role": "user", "content": user_message})
15
+ payload = {
16
+ "inputs": {
17
+ "past_user_inputs": [m["content"] for m in chat_history if m["role"] == "user"][:-1],
18
+ "generated_responses": [m["content"] for m in chat_history if m["role"] == "assistant"],
19
+ "text": user_message
20
+ },
21
+ "options": {"use_cache": False}
22
+ }
23
+
24
+ response = requests.post(API_URL, headers=HEADERS, json=payload)
25
+ response.raise_for_status()
26
+ result = response.json()
27
+
28
+ if isinstance(result, dict) and "generated_text" in result:
29
+ reply = result["generated_text"]
30
+ else:
31
+ reply = result[0].get("generated_text", "")
32
+
33
+ reply = reply.strip()
34
+ chat_history.append({"role": "assistant", "content": reply})
35
  return reply
36
 
37
  with gr.Blocks() as demo:
38
  chatbot_ui = gr.Chatbot()
39
+ msg = gr.Textbox(
40
+ placeholder="Type your message and press Enter",
41
+ lines=1,
42
+ show_label=False
43
+ )
44
+ msg.submit(fn=chat, inputs=msg, outputs=chatbot_ui)
45
 
46
+ if __name__ == "__main__":
47
+ demo.launch()