Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import discord
|
2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
DISCORD_TOKEN = os.environ.get("discord_key")
|
5 |
|
@@ -23,8 +119,8 @@ async def on_message(message):
|
|
23 |
return
|
24 |
|
25 |
# Respond with "pong" if the message contains "ping"
|
26 |
-
|
27 |
-
|
28 |
|
29 |
# Run the bot with your token
|
30 |
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import hashlib
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
import time
|
6 |
+
from langsmith import traceable
|
7 |
+
import random
|
8 |
import discord
|
9 |
import os
|
10 |
+
from transformers import pipeline
|
11 |
+
import torch
|
12 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
13 |
+
import numpy as np
|
14 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
15 |
+
from pydantic import BaseModel
|
16 |
+
from typing import List, Optional
|
17 |
+
from tqdm import tqdm
|
18 |
+
import re
|
19 |
+
import os
|
20 |
+
|
21 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
22 |
+
|
23 |
+
st.set_page_config(page_title="TeapotAI Discord Bot", page_icon=":robot_face:", layout="wide")
|
24 |
+
tokenizer = None
|
25 |
+
model = None
|
26 |
+
model_name = "teapotai/teapotllm"
|
27 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
28 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
def log_time(func):
|
33 |
+
def wrapper(*args, **kwargs):
|
34 |
+
start_time = time.time()
|
35 |
+
result = func(*args, **kwargs)
|
36 |
+
end_time = time.time()
|
37 |
+
print(f"{func.__name__} executed in {end_time - start_time:.4f} seconds")
|
38 |
+
return result
|
39 |
+
return wrapper
|
40 |
+
|
41 |
+
|
42 |
+
API_KEY = os.environ.get("brave_api_key")
|
43 |
+
|
44 |
+
@log_time
|
45 |
+
def brave_search(query, count=3):
|
46 |
+
url = "https://api.search.brave.com/res/v1/web/search"
|
47 |
+
headers = {"Accept": "application/json", "X-Subscription-Token": API_KEY}
|
48 |
+
params = {"q": query, "count": count}
|
49 |
+
|
50 |
+
response = requests.get(url, headers=headers, params=params)
|
51 |
+
|
52 |
+
if response.status_code == 200:
|
53 |
+
results = response.json().get("web", {}).get("results", [])
|
54 |
+
print(results)
|
55 |
+
return [(res["title"], res["description"], res["url"]) for res in results]
|
56 |
+
else:
|
57 |
+
print(f"Error: {response.status_code}, {response.text}")
|
58 |
+
return []
|
59 |
+
|
60 |
+
@traceable
|
61 |
+
@log_time
|
62 |
+
def query_teapot(prompt, context, user_input):
|
63 |
+
input_text = prompt + "\n" + context + "\n" + user_input
|
64 |
+
|
65 |
+
start_time = time.time()
|
66 |
+
|
67 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
68 |
+
input_length = inputs["input_ids"].shape[1]
|
69 |
+
|
70 |
+
output = model.generate(**inputs, max_new_tokens=512)
|
71 |
+
|
72 |
+
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
73 |
+
total_length = output.shape[1] # Includes both input and output tokens
|
74 |
+
output_length = total_length - input_length # Extract output token count
|
75 |
+
|
76 |
+
end_time = time.time()
|
77 |
+
|
78 |
+
elapsed_time = end_time - start_time
|
79 |
+
tokens_per_second = total_length / elapsed_time if elapsed_time > 0 else float("inf")
|
80 |
+
|
81 |
+
return output_text
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
@log_time
|
86 |
+
def handle_chat(user_prompt, user_input):
|
87 |
+
|
88 |
+
results = brave_search(user_input)
|
89 |
+
|
90 |
+
documents = [desc.replace('<strong>','').replace('</strong>','') for _, desc, _ in results]
|
91 |
+
|
92 |
+
context = "\n".join(documents)
|
93 |
+
prompt = """You are Teapot, an open-source AI assistant optimized for low-end devices, providing short, accurate responses without hallucinating while excelling at information extraction and text summarization. If a user asks who you are reply "I am Teapot"."""
|
94 |
+
response = query_teapot(prompt, context+user_prompt, user_input)
|
95 |
+
|
96 |
+
return response
|
97 |
+
|
98 |
+
st.write("418 I'm a teapot")
|
99 |
|
100 |
DISCORD_TOKEN = os.environ.get("discord_key")
|
101 |
|
|
|
119 |
return
|
120 |
|
121 |
# Respond with "pong" if the message contains "ping"
|
122 |
+
response = handle_chat(message.content)
|
123 |
+
await message.channel.send(response)
|
124 |
|
125 |
# Run the bot with your token
|
126 |
|