Spaces:
Build error
Build error
Commit ·
04f9073
1
Parent(s): c7fb8db
Update space
Browse files- README.md +1 -1
- app.py +83 -32
- history.py +26 -0
- mongo_client.py +15 -0
- requirements.txt +5 -1
- works.py +108 -0
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 💬
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Tuned Llama Ai Interviewer
|
| 3 |
emoji: 💬
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
app.py
CHANGED
|
@@ -1,52 +1,102 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
| 12 |
-
history: list[
|
| 13 |
-
system_message,
|
| 14 |
max_tokens,
|
| 15 |
temperature,
|
| 16 |
top_p,
|
|
|
|
| 17 |
):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
|
|
|
|
|
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are
|
| 50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
gr.Slider(
|
|
@@ -55,10 +105,11 @@ demo = gr.ChatInterface(
|
|
| 55 |
value=0.95,
|
| 56 |
step=0.05,
|
| 57 |
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
|
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from typing import List, Union, Dict, Tuple
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from os import getenv
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
+
|
| 7 |
+
from history import get_history, update_history
|
| 8 |
+
|
| 9 |
+
# Login to Hugging Face
|
| 10 |
+
login(getenv("Token"))
|
| 11 |
+
|
| 12 |
+
#name of model on huggingFace
|
| 13 |
+
model="ikenna1234/EleutherAI_pythia_1b_rlhf"
|
| 14 |
+
|
| 15 |
+
# Define generator pipeline
|
| 16 |
+
generator = pipeline("text-generation", model=model)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
#Transform gradio history by breaking any tuple into 2 dicts
|
| 20 |
+
def transform_gradio_history(history: List[Union[Dict[str, str], Tuple[str, str]]]) -> List[Dict[str, str]]:
|
| 21 |
+
transformed_history = []
|
| 22 |
+
|
| 23 |
+
for entry in history:
|
| 24 |
+
if (isinstance(entry, list) or isinstance(entry, tuple)) and len(entry) == 2:
|
| 25 |
+
transformed_history.append({"role": "user", "content": entry[0]})
|
| 26 |
+
transformed_history.append({"role": "assistant", "content": entry[1]})
|
| 27 |
+
elif isinstance(entry, dict):
|
| 28 |
+
transformed_history.append(entry)
|
| 29 |
+
|
| 30 |
+
return transformed_history
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
#Does the actual inference and streams (yield) the response
|
| 34 |
+
def chat(history:list[dict[str, str]]):
|
| 35 |
+
for msg in generator(
|
| 36 |
+
history, #message list
|
| 37 |
+
max_new_tokens=10048,
|
| 38 |
+
return_full_text=False
|
| 39 |
+
):
|
| 40 |
+
yield msg['generated_text']
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
def respond(
|
| 45 |
message,
|
| 46 |
+
history: list[dict[str, str]],
|
| 47 |
+
system_message, #system prompt
|
| 48 |
max_tokens,
|
| 49 |
temperature,
|
| 50 |
top_p,
|
| 51 |
+
group_name #user Id
|
| 52 |
):
|
| 53 |
+
if not group_name:
|
| 54 |
+
#user must pass user Id to the group_name.
|
| 55 |
+
#This is used to identify the user
|
| 56 |
+
yield "User ID required"
|
| 57 |
+
else:
|
| 58 |
+
messages=history
|
| 59 |
+
|
| 60 |
+
#If no history, get history from database
|
| 61 |
+
if not len(messages):
|
| 62 |
+
messages=get_history(group_name)
|
| 63 |
+
|
| 64 |
+
#Break any tuples into 2 dicts
|
| 65 |
+
messages=transform_gradio_history(messages)
|
| 66 |
+
|
| 67 |
+
#Add prompt to list of messages
|
| 68 |
+
messages.append({"role": "user", "content": message})
|
| 69 |
|
| 70 |
+
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
#Create new list of all messages, starting with system prompt
|
| 73 |
+
mainMessage=[{"role": "system", "content": system_message}, *messages]
|
| 74 |
|
| 75 |
+
#calls the inference function and streams the response
|
| 76 |
+
for msg in chat(mainMessage):
|
| 77 |
+
token = msg
|
| 78 |
|
| 79 |
+
# This is a stream. Meaning response comes in bits of string.
|
| 80 |
+
# Add new response string bit to previous response
|
| 81 |
+
# strings to form the whole string
|
| 82 |
+
response += token
|
| 83 |
+
yield response
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
#update the history in database
|
| 86 |
+
if response:
|
| 87 |
+
messages.append({"role": "assistant", "content": response})
|
| 88 |
+
update_history(group_name,messages)
|
| 89 |
|
| 90 |
+
def initialize():
|
| 91 |
+
messages=[]
|
| 92 |
+
return messages
|
| 93 |
|
|
|
|
|
|
|
|
|
|
| 94 |
demo = gr.ChatInterface(
|
| 95 |
respond,
|
| 96 |
+
type="messages",
|
| 97 |
+
chatbot=gr.Chatbot(value=initialize(),type="messages"),
|
| 98 |
additional_inputs=[
|
| 99 |
+
gr.Textbox(value="You are an AI assistant that conducts interview", label="System message"),
|
| 100 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 101 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 102 |
gr.Slider(
|
|
|
|
| 105 |
value=0.95,
|
| 106 |
step=0.05,
|
| 107 |
label="Top-p (nucleus sampling)",
|
| 108 |
+
),
|
| 109 |
+
gr.Textbox( label="User ID"),
|
| 110 |
],
|
| 111 |
)
|
| 112 |
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
+
demo.launch(share=True,ssr_mode=False)
|
history.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mongo_client import get_client, get_collection
|
| 2 |
+
|
| 3 |
+
client=get_client()
|
| 4 |
+
collection=get_collection("history",client)
|
| 5 |
+
|
| 6 |
+
def get_history(group_name:str):
|
| 7 |
+
history= collection.find_one({"group_name":group_name})
|
| 8 |
+
|
| 9 |
+
value=[]
|
| 10 |
+
|
| 11 |
+
if history and 'value' in history:
|
| 12 |
+
value=history['value']
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
return value
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def update_history(group_name:str,value):
|
| 19 |
+
query_filter = {"group_name":group_name}
|
| 20 |
+
|
| 21 |
+
update_operation = { "$set" :{ "value" : value }}
|
| 22 |
+
|
| 23 |
+
collection.update_one(query_filter, update_operation, upsert=True)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
return True
|
mongo_client.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pymongo import MongoClient, server_api
|
| 2 |
+
|
| 3 |
+
from os import getenv
|
| 4 |
+
|
| 5 |
+
database_name=getenv("DATABASE_NAME")
|
| 6 |
+
|
| 7 |
+
def get_client():
|
| 8 |
+
client = MongoClient(getenv("MONGODB_CONNECTION_STRING"), server_api=server_api.ServerApi(
|
| 9 |
+
version="1", strict=True, deprecation_errors=True))
|
| 10 |
+
|
| 11 |
+
return client
|
| 12 |
+
|
| 13 |
+
def get_collection(collection_name:str,client:MongoClient):
|
| 14 |
+
database=client[database_name]
|
| 15 |
+
return database[collection_name]
|
requirements.txt
CHANGED
|
@@ -1 +1,5 @@
|
|
| 1 |
-
huggingface_hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub
|
| 2 |
+
transformers>=4.48
|
| 3 |
+
pymongo
|
| 4 |
+
torch
|
| 5 |
+
accelerate
|
works.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
from typing import List, Union, Dict, Tuple
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
from os import getenv
|
| 7 |
+
from huggingface_hub import login
|
| 8 |
+
|
| 9 |
+
from history import get_history, update_history
|
| 10 |
+
|
| 11 |
+
# Login to Hugging Face
|
| 12 |
+
login(getenv("Token"))
|
| 13 |
+
|
| 14 |
+
#client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 15 |
+
|
| 16 |
+
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
| 17 |
+
|
| 18 |
+
generator = pipeline("text-generation", model="ikenna1234/ai_interviewer_2")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def transform_gradio_history(history: List[Union[Dict[str, str], Tuple[str, str]]]) -> List[Dict[str, str]]:
|
| 22 |
+
transformed_history = []
|
| 23 |
+
|
| 24 |
+
for entry in history:
|
| 25 |
+
if (isinstance(entry, list) or isinstance(entry, tuple)) and len(entry) == 2:
|
| 26 |
+
transformed_history.append({"role": "user", "content": entry[0]})
|
| 27 |
+
transformed_history.append({"role": "assistant", "content": entry[1]})
|
| 28 |
+
elif isinstance(entry, dict):
|
| 29 |
+
transformed_history.append(entry)
|
| 30 |
+
|
| 31 |
+
return transformed_history
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def chat(history:list[dict[str, str]]):
|
| 35 |
+
for msg in generator(
|
| 36 |
+
history,
|
| 37 |
+
max_new_tokens=10048,
|
| 38 |
+
return_full_text=False
|
| 39 |
+
):
|
| 40 |
+
yield msg[0]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def respond(
|
| 45 |
+
message,
|
| 46 |
+
history: list[dict[str, str]],
|
| 47 |
+
system_message,
|
| 48 |
+
max_tokens,
|
| 49 |
+
temperature,
|
| 50 |
+
top_p,
|
| 51 |
+
group_name
|
| 52 |
+
):
|
| 53 |
+
if not group_name:
|
| 54 |
+
yield "User ID required"
|
| 55 |
+
else:
|
| 56 |
+
messages=history
|
| 57 |
+
|
| 58 |
+
#If no history, get history from database
|
| 59 |
+
if not len(messages):
|
| 60 |
+
messages=get_history(group_name)
|
| 61 |
+
#messages=old_history
|
| 62 |
+
|
| 63 |
+
#Break any tuples into 2 dicts
|
| 64 |
+
messages=transform_gradio_history(messages)
|
| 65 |
+
|
| 66 |
+
messages.append({"role": "user", "content": message})
|
| 67 |
+
|
| 68 |
+
response = ""
|
| 69 |
+
|
| 70 |
+
mainMessage=[{"role": "system", "content": system_message}, *messages]
|
| 71 |
+
|
| 72 |
+
for msg in chat(mainMessage):
|
| 73 |
+
token = msg
|
| 74 |
+
|
| 75 |
+
response += token
|
| 76 |
+
yield response
|
| 77 |
+
|
| 78 |
+
#update the history in database
|
| 79 |
+
if response:
|
| 80 |
+
messages.append({"role": "assistant", "content": response})
|
| 81 |
+
update_history(group_name,messages)
|
| 82 |
+
|
| 83 |
+
def initialize():
|
| 84 |
+
messages=[]
|
| 85 |
+
return messages
|
| 86 |
+
|
| 87 |
+
demo = gr.ChatInterface(
|
| 88 |
+
respond,
|
| 89 |
+
type="messages",
|
| 90 |
+
chatbot=gr.Chatbot(value=initialize(),type="messages"),
|
| 91 |
+
additional_inputs=[
|
| 92 |
+
gr.Textbox(value="You are an AI assistant that conducts interview", label="System message"),
|
| 93 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 94 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 95 |
+
gr.Slider(
|
| 96 |
+
minimum=0.1,
|
| 97 |
+
maximum=1.0,
|
| 98 |
+
value=0.95,
|
| 99 |
+
step=0.05,
|
| 100 |
+
label="Top-p (nucleus sampling)",
|
| 101 |
+
),
|
| 102 |
+
gr.Textbox( label="User ID"),
|
| 103 |
+
],
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
demo.launch(share=True,ssr_mode=False)
|