Anasuya Basu commited on
Commit
2ccf26b
·
1 Parent(s): c97a8a3

Trying to add back open ai

Browse files
Files changed (2) hide show
  1. app.py +39 -0
  2. requirements.txt +7 -1
app.py CHANGED
@@ -1,5 +1,10 @@
1
  import gradio as gr
 
 
2
  import os
 
 
 
3
  from huggingface_hub import InferenceClient
4
  from huggingface_hub import login
5
 
@@ -9,6 +14,40 @@ For more information on `huggingface_hub` Inference API support, please check th
9
  login(token=os.getenv("HF_TOKEN"))
10
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def respond(
14
  message,
 
1
  import gradio as gr
2
+ from openai import OpenAI
3
+ import json
4
  import os
5
+ import requests
6
+ from pypdf import PdfReader
7
+ import gradio as gr
8
  from huggingface_hub import InferenceClient
9
  from huggingface_hub import login
10
 
 
14
  login(token=os.getenv("HF_TOKEN"))
15
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
16
 
17
+ class Harold:
18
+
19
+ def __init__(self):
20
+ self.openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
21
+ self.name = "Harold"
22
+ reader = PdfReader("data/Living-Playbook.pdf")
23
+ self.text = ""
24
+ for page in reader.pages:
25
+ text = page.extract_text()
26
+ if text:
27
+ self.text += text
28
+
29
+ def system_prompt(self):
30
+ system_prompt = f"""
31
+ You are acting as {self.name}, a helpful assistant.
32
+ You are answering questions and having discussions about the contents of the book "Living Playbook".
33
+ Be friendly and approachable but also consise and to the point. If you don't know the answer, say so.
34
+ You might be asked to explain a concept or idea in the book and describe a purpose of a game. You should be able to do this.
35
+ """
36
+ system_prompt += f"""
37
+ Here is the context of the book:
38
+ {self.text}
39
+ """
40
+ return system_prompt
41
+
42
+ def chat(self, message, history):
43
+ messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
44
+
45
+ response = self.openai_client.chat.completions.create(
46
+ model="gpt-4o-mini",
47
+ messages=messages,
48
+ )
49
+ return response.choices[0].message.content
50
+
51
 
52
  def respond(
53
  message,
requirements.txt CHANGED
@@ -1 +1,7 @@
1
- huggingface_hub==0.25.2
 
 
 
 
 
 
 
1
+ huggingface_hub==0.25.2
2
+ requests
3
+ python-dotenv
4
+ gradio
5
+ pypdf
6
+ openai
7
+ openai-agents