Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,37 @@
|
|
| 1 |
import subprocess
|
| 2 |
-
subprocess.check_call(["pip", "install", "openai"])
|
| 3 |
-
subprocess.check_call(["pip", "install", "gradio", "transformers", "python-dotenv"
|
| 4 |
-
import random
|
| 5 |
import gradio as gr
|
| 6 |
-
from
|
| 7 |
import openai
|
| 8 |
-
import
|
| 9 |
import os
|
| 10 |
-
'''
|
| 11 |
-
openai.api_type = os.environ.get("OPENAI_API_KEY")
|
| 12 |
-
openai.api_base = os.environ.get("OPENAI_API_KEY")
|
| 13 |
-
openai.api_version = os.environ.get("OPENAI_API_KEY")
|
| 14 |
-
'''
|
| 15 |
-
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
title = "π Chat with Pigeon"
|
| 46 |
-
|
| 47 |
-
description = \
|
| 48 |
-
"""
|
| 49 |
-
π¬ This space is powered by [**PigeonLLM**](https://huggingface.co/openskyml/pigeon-llm).
|
| 50 |
-
|
| 51 |
-
π This space runs **very fast** even on **CPU**.
|
| 52 |
-
|
| 53 |
-
π You get totally unique and creative answers.
|
| 54 |
-
|
| 55 |
-
π PigeonChat is available worldwide in over **160 languages**.
|
| 56 |
-
|
| 57 |
-
π PigeonChat is powered by **open source** and is completely **private**.
|
| 58 |
-
|
| 59 |
-
π₯οΈοΈ This demo is by **Evgeniy Hristoforu** (**OpenSkyML**).
|
| 60 |
-
|
| 61 |
-
<h2></h2>
|
| 62 |
-
"""
|
| 63 |
-
|
| 64 |
-
if not torch.cuda.is_available():
|
| 65 |
-
description += """\n<p style='text-align: center'>π Running on CPU!</p>"""
|
| 66 |
-
else:
|
| 67 |
-
description += """\n<p style='text-align: center'>π Running on powerful hardware!</p>"""
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
examples=[
|
| 71 |
-
'Hello there! How are you doing?',
|
| 72 |
-
'Can you explain briefly to me what is the Python programming language?',
|
| 73 |
-
'Explain the plot of Cinderella in a sentence.',
|
| 74 |
-
'How many hours does it take a man to eat a Helicopter?',
|
| 75 |
-
"Write a 100-word article on 'Benefits of Open-Source in AI research'",
|
| 76 |
-
]
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
gr.ChatInterface(gptresponse, title=title, description=description, examples=examples).launch()
|
|
|
|
| 1 |
import subprocess
|
| 2 |
+
subprocess.check_call(["pip", "install", "-q", "openai"])
|
| 3 |
+
subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv"])
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
+
from transformers import TFAutoModelForCausalLM, AutoTokenizer
|
| 6 |
import openai
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
load_dotenv() # load environment variables from .env file
|
| 11 |
+
api_key = os.getenv("OPENAI_API_KEY") # access the value of the OPENAI_API_KEY environment variable
|
| 12 |
+
|
| 13 |
+
def openai_chat(prompt):
|
| 14 |
+
if "who are you" in prompt.lower() or "your name" in prompt.lower() or "name" in prompt.lower():
|
| 15 |
+
return "My name is ChatSherman. How can I assist you today?"
|
| 16 |
+
else:
|
| 17 |
+
prompt = "I'm an AI chatbot named ChatSherman designed by a student named ShermanAI at the Department of Electronic and Information Engineering at The Hong Kong Polytechnic University to help you with your engineering questions. Also, I can assist with a wide range of topics and questions." + prompt
|
| 18 |
+
completions = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=1024, n=1, temperature=0.5,)
|
| 19 |
+
message = completions.choices[0].text
|
| 20 |
+
return message.strip()
|
| 21 |
+
|
| 22 |
+
def chatbot(talk_to_chatsherman, history=[]):
|
| 23 |
+
output = openai_chat(talk_to_chatsherman)
|
| 24 |
+
history.append((talk_to_chatsherman, output))
|
| 25 |
+
return history, history
|
| 26 |
+
|
| 27 |
+
title = "ChatSherman"
|
| 28 |
+
description = "This is an AI chatbot powered by ShermanAI. Enter your question below to get started."
|
| 29 |
+
examples = [
|
| 30 |
+
["What is ChatSherman, and how does it work?", []],
|
| 31 |
+
["Is my personal information and data safe when I use the ChatSherman chatbot?", []],
|
| 32 |
+
["What are some common applications of deep learning in engineering?", []]
|
| 33 |
+
]
|
| 34 |
+
inputs = [gr.inputs.Textbox(label="Talk to ChatSherman: "), "state"]
|
| 35 |
+
outputs = ["chatbot", "state"]
|
| 36 |
+
interface = gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples)
|
| 37 |
+
interface.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|