File size: 4,071 Bytes
4637859
d3e7d7f
cc33446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3e7d7f
cc33446
d3e7d7f
cc33446
d3e7d7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import gradio as gr

def test(x, request: gr.Request):
    return request.query_params

gr.Interface(test, "textbox", "textbox").launch()  



# from openai import OpenAI
# import gradio as gr
# import os, json

# # Attempt to load configuration from config.json
# try:
#     with open('config.json') as config_file:
#         config = json.load(config_file)
#     OPENAI_API_KEY = config.get("OPENAI_API_KEY")
#     SYSTEM_PROMPT = config.get("SYSTEM_PROMPT")
# except FileNotFoundError:
#     # If config.json is not found, fall back to environment variables
#     OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
#     SYSTEM_PROMPT = os.getenv("SYSTEM_PROMPT")

# # Fallback to default values if necessary
# if not OPENAI_API_KEY:
#     raise ValueError("OPENAI_API_KEY is not set in config.json or as an environment variable.")
# if not SYSTEM_PROMPT:
#     SYSTEM_PROMPT = "This is a default system prompt."

# client = OpenAI(api_key=OPENAI_API_KEY)

# MODEL = "gpt-3.5-turbo"

# def predict(message):
#     system_prompt = {
#         "role": "system",
#         "content": SYSTEM_PROMPT
#     }
#     history_openai_format = [system_prompt]
#     history_openai_format.append({"role": "user", "content": message})

#     response = client.chat.completions.create(model=MODEL,
#                                               messages=history_openai_format,
#                                               temperature=1.0,
#                                               max_tokens=150,
#                                               stream=False)
#     return response.choices[0].message['content']

# # JavaScript function to get the question from URL on load
# js_on_load = """
# function() {
#     const params = new URLSearchParams(window.location.search);
#     const question = params.get("question") || "Enter your question here";
#     return [question];
# }
# """

# with gr.Blocks() as app:
#     with gr.Row():
#         question_input = gr.Textbox(label="Your Question", placeholder="Enter your question here")
#         submit_button = gr.Button("Submit")
#     answer_output = gr.Textbox(label="Answer", interactive=False)

#     submit_button.click(fn=predict, inputs=question_input, outputs=answer_output)
    
#     app.load(js=js_on_load)  # Load the question from URL on startup

# app.launch(share=True, debug=True)

###__________________________________________
# V0
# from openai import OpenAI
# import gradio as gr
# import os, json
# # Attempt to load configuration from config.json
# try:
#     with open('config.json') as config_file:
#         config = json.load(config_file)
#     OPENAI_API_KEY = config.get("OPENAI_API_KEY")
#     SYSTEM_PROMPT = config.get("SYSTEM_PROMPT")
# except FileNotFoundError:
#     # If config.json is not found, fall back to environment variables
#     OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
#     SYSTEM_PROMPT = os.getenv("SYSTEM_PROMPT")

# # Fallback to default values if necessary
# if not OPENAI_API_KEY:
#     raise ValueError("OPENAI_API_KEY is not set in config.json or as an environment variable.")
# if not SYSTEM_PROMPT:
#     SYSTEM_PROMPT = "This is a default system prompt."

# client = OpenAI(api_key=OPENAI_API_KEY)

# system_prompt = {
#     "role": "system",
#     "content": SYSTEM_PROMPT
# }


# MODEL = "gpt-3.5-turbo"

# def predict(message, history):
#     history_openai_format = [system_prompt]
#     for human, assistant in history:
#         history_openai_format.append({"role": "user", "content": human })
#         history_openai_format.append({"role": "assistant", "content":assistant})
#     history_openai_format.append({"role": "user", "content": message})

#     response = client.chat.completions.create(model=MODEL,
#     messages= history_openai_format,
#     temperature=1.0,
#     stream=True)

#     partial_message = ""
#     for chunk in response:
#         if chunk.choices[0].delta.content:
#             partial_message = partial_message + chunk.choices[0].delta.content
#             yield partial_message

# gr.ChatInterface(predict).launch(share=True)