Spaces:
Runtime error
Runtime error
clean code
Browse files
app.py
CHANGED
@@ -17,101 +17,21 @@ client = OpenAI(
|
|
17 |
api_key=os.getenv('OPENAI_API_KEY')
|
18 |
)
|
19 |
|
20 |
-
# hugging face setup
|
21 |
-
#model_name = "mmnga/ELYZA-japanese-Llama-2-7b-instruct-gguf"
|
22 |
-
API_URL = f"https://api-inference.huggingface.co/models/"
|
23 |
-
#API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
|
24 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
25 |
-
|
26 |
# Global variable to control debug printing
|
27 |
DEBUG_MODE = True
|
28 |
|
29 |
|
30 |
-
|
31 |
-
|
32 |
def debug_print(*args, **kwargs):
|
33 |
if DEBUG_MODE:
|
34 |
print(*args, **kwargs)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
def translate_openai(input_text):
|
39 |
-
|
40 |
-
prompt = "Translate the following text into Japanese language: " + input_text
|
41 |
-
|
42 |
-
response = client.chat.completions.create( # get translation from GPT
|
43 |
-
messages=[
|
44 |
-
{
|
45 |
-
"role": "user",
|
46 |
-
"content": prompt,
|
47 |
-
}
|
48 |
-
],
|
49 |
-
model="gpt-3.5-turbo",
|
50 |
-
temperature=0 # should be the same translation every time
|
51 |
-
)
|
52 |
-
translation = response.choices[0].message.content
|
53 |
-
debug_print("GPT translation:", translation)
|
54 |
-
|
55 |
-
return translation
|
56 |
-
|
57 |
-
def assess(original_japanese, student_translation):
|
58 |
-
|
59 |
-
try:
|
60 |
-
# get the English translation
|
61 |
-
generated_translation = translate_hf(original_japanese)
|
62 |
-
debug_print("Generated translation:", generated_translation)
|
63 |
-
except Exception as e:
|
64 |
-
return "Error in processing translation.", str(e)
|
65 |
-
|
66 |
-
try:
|
67 |
-
prompt = (f"Evaluate the student's English translation of Japanese for accuracy and naturalness. "
|
68 |
-
f"Original: {original_japanese}, "
|
69 |
-
f"Reference Translation: {generated_translation}, "
|
70 |
-
f"Student Translation: {student_translation}. "
|
71 |
-
"Highlight errors, suggest improvements, and note any nuances. Provide concise and very simple feedback for an English language learner aimed at improving their translation skills. Where possible, give concrete examples.")
|
72 |
-
|
73 |
-
debug_print(prompt)
|
74 |
-
|
75 |
-
# Evaluating the student's translation attempt
|
76 |
-
response = client.chat.completions.create(
|
77 |
-
messages=[
|
78 |
-
{
|
79 |
-
"role": "user",
|
80 |
-
"content": prompt,
|
81 |
-
}
|
82 |
-
],
|
83 |
-
model="gpt-3.5-turbo",
|
84 |
-
)
|
85 |
-
|
86 |
-
debug_print("Full GPT response:", response)
|
87 |
-
|
88 |
-
debug_print("Generated translation:", generated_translation)
|
89 |
-
|
90 |
-
evaluation_feedback = response.choices[0].message.content
|
91 |
-
|
92 |
-
return generated_translation, evaluation_feedback
|
93 |
-
except Exception as e:
|
94 |
-
return "Error in processing evaluation.", str(e)
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
def generate_questions(input_prompt):
|
99 |
"""
|
100 |
Generates EFL questions based on the input text using OpenAI's GPT-3.
|
101 |
"""
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
# temperature=0.5,
|
106 |
-
# max_tokens=150,
|
107 |
-
# top_p=1.0,
|
108 |
-
# frequency_penalty=0.0,
|
109 |
-
# presence_penalty=0.0
|
110 |
-
# )
|
111 |
-
# return response.choices[0].text.strip()
|
112 |
-
|
113 |
-
|
114 |
-
prompt=f"Generate 5 EFL (English as a Foreign Language) simple questions based on the following topic: {input_prompt}."
|
115 |
|
116 |
response = client.chat.completions.create(
|
117 |
|
|
|
17 |
api_key=os.getenv('OPENAI_API_KEY')
|
18 |
)
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# Global variable to control debug printing
|
21 |
DEBUG_MODE = True
|
22 |
|
23 |
|
|
|
|
|
24 |
def debug_print(*args, **kwargs):
|
25 |
if DEBUG_MODE:
|
26 |
print(*args, **kwargs)
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def generate_questions(input_prompt):
|
29 |
"""
|
30 |
Generates EFL questions based on the input text using OpenAI's GPT-3.
|
31 |
"""
|
32 |
+
|
33 |
+
|
34 |
+
prompt=f"Generate 5 simple questions for an EFL (English as a Foreign Language) based on the following topic: {input_prompt}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
response = client.chat.completions.create(
|
37 |
|